Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SetNavigationButtonsInput
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 collectFromSetConnections
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3class SetNavigationButtonsInput
4{
5    public function __construct($setFunction)
6    {
7        $this->setFunction = $setFunction;
8    }
9
10    public function execute(TsumegoButtons $tsumegoButtons, array $currentSetConnection): void
11    {
12        $navigationButtons = $this->collectFromSetConnections($tsumegoButtons, $currentSetConnection);
13        ($this->setFunction)('tsumegoButtons', $navigationButtons);
14    }
15
16    private function collectFromSetConnections(TsumegoButtons $tsumegoButtons, array $currentSetConnection): TsumegoButtons
17    {
18        $result = TsumegoButtons::deriveFrom($tsumegoButtons);
19        $currentIndex = array_find_key((array) $tsumegoButtons, function ($tsumegoButton) use ($currentSetConnection) { return $tsumegoButton->setConnectionID === $currentSetConnection['SetConnection']['id']; });
20
21        // mark the problem we are going to visit as already visited
22        if ($tsumegoButtons[$currentIndex]->status == 'N')
23            $tsumegoButtons[$currentIndex]->status = 'V';
24
25        if (count($tsumegoButtons) <= self::$NEIGHBOUR_COUNT_TO_SHOW_ON_EACH_SIDE * 2 + 3)
26        {
27            foreach ($tsumegoButtons as $tsumegoButton)
28                $result [] = $tsumegoButton;
29            return $result;
30        }
31        $startIndex = max(1, $currentIndex - self::$NEIGHBOUR_COUNT_TO_SHOW_ON_EACH_SIDE);
32        $endIndex = min($startIndex + self::$NEIGHBOUR_COUNT_TO_SHOW_ON_EACH_SIDE * 2, count($tsumegoButtons) - 2);
33        $startIndex = max(1, $endIndex - self::$NEIGHBOUR_COUNT_TO_SHOW_ON_EACH_SIDE * 2);
34        $result [] = $tsumegoButtons[0];
35        for ($i = $startIndex; $i <= $endIndex; $i++)
36            $result [] = $tsumegoButtons[$i];
37        $result [] = $tsumegoButtons[count($tsumegoButtons) - 1];
38        return $result;
39    }
40
41    private static $NEIGHBOUR_COUNT_TO_SHOW_ON_EACH_SIDE = 5;
42    private $setFunction;
43}