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

SprintService::getSprintFromJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: meshulam
5
 * Date: 11/08/2017
6
 * Time: 17:28.
7
 */
8
9
namespace JiraRestApi\Sprint;
10
11
use JiraRestApi\Configuration\ConfigurationInterface;
12
use JiraRestApi\JiraClient;
13
use JiraRestApi\JiraException;
14
use Monolog\Logger;
15
16
class SprintService
17
{
18
    private $uri = '/rest/agile/1.0/sprint';
19
20
    protected $restClient;
21
22
    public function __construct(ConfigurationInterface $configuration = null, Logger $logger = null, $path = './', JiraClient $jiraClient = null)
23
    {
24
        $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...
25
        $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...
26
        $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...
27
        $this->restClient = $jiraClient;
28
        if (!$this->restClient) {
29
            $this->setRestClient();
30
        }
31
    }
32
33
    public function setRestClient()
34
    {
35
        $this->restClient = new JiraClient($this->configuration, $this->logger, $this->path);
36
        $this->restClient->setAPIUri('');
37
    }
38
39
40
    public static function getSprintFromJSON($json)
41
    {
42
        $json_mapper = new \JsonMapper();
43
        $json_mapper->undefinedPropertyHandler = [new \JiraRestApi\JsonMapperHelper(), 'setUndefinedProperty'];
44
45
        return $json_mapper->map($json, new Sprint());
46
    }
47
48
    /**
49
     *  get all Sprint list.
50
     *
51
     * @param int $sprintId
52
     *
53
     * @throws JiraException
54
     * @throws \JsonMapper_Exception
55
     *
56
     * @return object
57
     */
58
    public function getSprint($sprintId)
59
    {
60
        $ret = $this->restClient->exec($this->uri.'/'.$sprintId);
61
62
        return $this->getSprintFromJSON(json_decode($ret));
63
    }
64
65
    public function getVelocityForSprint($sprintID)
66
    {
67
        try {
68
            $sprint = $this->getSprint($sprintID);
69
            if (!is_null($sprint->originBoardId)) {
70
                $ret = $this->restClient->exec('/rest/greenhopper/1.0/rapid/charts/velocity.json?rapidViewId='.$sprint->originBoardId.'&sprintId='.$sprint->id);
71
                $velocityObject = json_decode($ret);
72
                $velocityStats = $velocityObject->{'velocityStatEntries'};
73
                if (property_exists($velocityStats, $sprint->id)) {
74
                    $sprint->estimatedVelocity = $velocityStats->{$sprint->id}->{'estimated'}->value;
75
                    $sprint->completedVelocity = $velocityStats->{$sprint->id}->{'completed'}->value;
76
                } else {
77
                    $sprint->estimatedVelocity = null;
78
                    $sprint->completedVelocity = null;
79
                }
80
            }
81
82
            return $sprint;
83
        } catch (JiraException $e) {
84
            print("Error Occured! " . $e->getMessage());
85
86
            return;
87
        }
88
    }
89
}
90