Completed
Push — master ( dbfa6d...65a5eb )
by Bernhard
16s queued 12s
created

Jira::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * \Wicked\Timely\PushServices\Jira
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    wick-ed
15
 * @copyright 2020 Bernhard Wick
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/wick-ed/timely
18
 */
19
20
namespace Wicked\Timely\PushServices;
21
22
use JiraRestApi\Issue\IssueService;
23
use JiraRestApi\Issue\Worklog;
24
use Wicked\Timely\DotEnvConfiguration;
25
use Wicked\Timely\Entities\Task;
26
use Wicked\Timely\Helper\Date;
27
use Wicked\Timely\PushServices\Authentication\PasswordRetrievalStrategyInterface;
28
29
/**
30
 * Jira push service
31
 *
32
 * @author    wick-ed
33
 * @copyright 2020 Bernhard Wick
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/wick-ed/timely
36
 */
37
class Jira implements PushServiceInterface, AuthenticationAwarePushServiceInterface
38
{
39
40
    /**
41
     * @var IssueService
42
     */
43
    protected $issueService;
44
45
    /**
46
     * @var PasswordRetrievalStrategyInterface
47
     */
48
    protected $passwordRetrievalStrategy;
49
50
    /**
51
     * Jira constructor.
52
     *
53
     * @param PasswordRetrievalStrategyInterface $passwordRetrievalStrategy
54
     *
55
     * @throws \JiraRestApi\JiraException
56
     */
57
    public function __construct(PasswordRetrievalStrategyInterface $passwordRetrievalStrategy)
58
    {
59
        $this->passwordRetrievalStrategy = $passwordRetrievalStrategy;
60
        $this->init();
61
    }
62
63
    /**
64
     * Initialize
65
     *
66
     * @throws \JiraRestApi\JiraException
67
     */
68
    protected function init()
69
    {
70
        // retrieve configuration
71
        $configuration = new DotEnvConfiguration();
72
        $password = $configuration->getJiraPassword();
73
        if (empty($password)) {
74
            $password = $this->passwordRetrievalStrategy->getPassword();
75
            //$password = $this->getPasswordFromKeychain($configuration);
76
            if (empty($password)) {
77
                return;
78
            }
79
            $configuration->setJiraPassword($password);
80
        }
81
        $this->issueService = new IssueService($configuration);
82
    }
83
84
    /**
85
     * Push a given task to a (remote) service
86
     *
87
     * @param Task $task
88
     *
89
     * @return boolean
90
     *
91
     * @throws \Exception
92
     */
93
    public function push(Task $task)
94
    {
95
        // create a worklog from the task
96
        $workLog = new Worklog();
97
        $workLog->setComment($task->getComment())
98
            ->setStarted($task->getStartTime())
99
            ->setTimeSpent(Date::secondsToUnits(Date::roundByInterval($task->getDuration(), 900)));
100
101
        // check the sanity of our worklog and discard it if there is something missing
102
        if (!$task->getTicketId() || empty($workLog->timeSpent) || empty($workLog->comment)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $task->getTicketId() of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
103
            throw new \Exception('Not pushing one worklog as it misses vital information');
104
        }
105
106
        // push to remote
107
        $this->issueService->addWorklog($task->getTicketId(), $workLog);
108
        return true;
109
    }
110
}
111