Completed
Pull Request — master (#193)
by
unknown
01:50
created

BoardService::getBoardFromJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JiraRestApi\Board;
4
5
use JiraRestApi\Configuration\ConfigurationInterface;
6
use JiraRestApi\JiraClient;
7
use JiraRestApi\Sprint\SprintService;
8
use Monolog\Logger;
9
10
class BoardService
11
{
12
    private $uri = '/rest/agile/1.0/board';
13
    protected $restClient;
14
15
    public function __construct(ConfigurationInterface $configuration = null, Logger $logger = null, $path = './', JiraClient $jiraClient = null)
16
    {
17
        $this->configuration = $configuration;
0 ignored issues
show
Bug Best Practice introduced by
The property configuration does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
        $this->logger = $logger;
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
        $this->path = $path;
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        $this->restClient = $jiraClient;
21
        if (!$this->restClient) {
22
            $this->setRestClient();
23
        }
24
    }
25
26
    public function setRestClient()
27
    {
28
        $this->restClient = new JiraClient($this->configuration, $this->logger, $this->path);
29
        $this->restClient->setAPIUri('');
30
    }
31
32
    public static function getBoardFromJSON($json)
33
    {
34
        $json_mapper = new \JsonMapper();
35
        $json_mapper->undefinedPropertyHandler = [new \JiraRestApi\JsonMapperHelper(), 'setUndefinedProperty'];
36
37
        return $json_mapper->map(
38
            $json,
39
            new Board()
40
        );
41
    }
42
43
    public static function getArrayOfBoardsFromJSON($boardList)
44
    {
45
        $json_mapper = new \JsonMapper();
46
        $json_mapper->undefinedPropertyHandler = [new \JiraRestApi\JsonMapperHelper(), 'setUndefinedProperty'];
47
48
        return $json_mapper->mapArray(
49
            $boardList,
50
            new \ArrayObject(),
51
            '\JiraRestApi\Board\Board'
52
            );
53
    }
54
55
    /**
56
     *  get a Board.
57
     *
58
     * @param int $boardId
59
     *
60
     * @return object
61
     */
62
    public function getBoard($boardId)
63
    {
64
        $ret = $this->restClient->exec('/rest/agile/1.0/board/'.$boardId, null);
65
66
        return $this->getBoardFromJSON(json_decode($ret));
67
    }
68
    /**
69
     *  get all Boards.
70
     *
71
     * @param int $boardId
72
     *
73
     * @return object
74
     */
75
    public function getAllBoards($paramArray = [])
76
    {
77
        $boardArray = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $boardArray is dead and can be removed.
Loading history...
78
        $boardArray = $this->loopOverResults($this->uri, $paramArray);
79
80
        return $this->getArrayOfBoardsFromJSON($boardArray);
81
    }
82
83
    public function getBoardWithSprintsList($boardId = null, $paramArray = [])
84
    {
85
        if (is_null($boardId)) {
86
            $boardList = $this->getAllBoards();
87
            $boards = [];
88
            foreach ($boardList as $board) {
89
                if ($board->type == 'scrum') {
90
                    $boards[] = $this->getSprintInfoForBoard($board->id, $paramArray);
91
                } else {
92
                    $boards[] = $board;
93
                }
94
            }
95
        } else {
96
            $boards[] = $this->getSprintInfoForBoard($boardId, $paramArray);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$boards was never initialized. Although not strictly required by PHP, it is generally a good practice to add $boards = array(); before regardless.
Loading history...
97
        }
98
99
        return $boards;
100
    }
101
102
    public function getSprintInfoForBoard($boardId, $paramArray)
103
    {
104
        $board = $this->getBoard($boardId);
105
        $sprintList = $this->loopOverResults($this->uri.'/'.$boardId.'/sprint/', $paramArray);
106
        $sprintObjectsArray = [];
107
        foreach ($sprintList as $sprint) {
108
            $sprintObjectsArray[$sprint->id] = SprintService::getSprintFromJSON($sprint);
109
        }
110
        $board->sprintList = $sprintObjectsArray;
111
112
        return $board;
113
    }
114
115
    public function loopOverResults($uri, $paramArray = [])
116
    {
117
        $resultsArray = [];
118
        $ret = $this->restClient->exec($uri.$this->restClient->toHttpQueryParameter($paramArray), null);
119
        $results = json_decode($ret);
120
        while (!$results->isLast) {
121
            $resultsArray = array_merge($resultsArray, $results->values);
122
            $paramArray['startAt'] = $results->startAt + $results->maxResults;
123
            $ret = $this->restClient->exec($this->uri.$this->restClient->toHttpQueryParameter($paramArray), null);
124
            $results = json_decode($ret);
125
        }
126
        
127
        return array_merge($resultsArray, $results->values);
128
    }
129
}
130