GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#68)
by Bidesh
02:54
created

ChronosApiClient::addingJob()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 14
cts 15
cp 0.9333
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 12
nc 7
nop 1
crap 7.0145
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-28
7
 *
8
 */
9
10
11
namespace Chapi\Component\RemoteClients;
12
13
14
use Chapi\Component\RemoteClients\ApiClientInterface;
15
use Chapi\Component\Http\HttpClientInterface;
16
use Chapi\Entity\Chronos\ChronosJobEntity;
17
use Chapi\Entity\JobEntityInterface;
18
use Chapi\Exception\ApiClientException;
19
use Symfony\Component\Config\Definition\Exception\Exception;
20
21
class ChronosApiClient implements ApiClientInterface
22
{
23
    /**
24
     * @var HttpClientInterface
25
     */
26
    private $oHttpClient;
27
28
    /**
29
     * @param HttpClientInterface $oHttpClient
30
     */
31 16
    public function __construct(
32
        HttpClientInterface $oHttpClient
33
    )
34
    {
35 16
        $this->oHttpClient = $oHttpClient;
36 16
    }
37
38
    /**
39
     * @inheritdoc
40
     * @link: https://mesos.github.io/chronos/docs/api.html#listing-jobs
41
     */
42 2
    public function listingJobs()
43
    {
44 2
        return $this->sendGetJsonRequest('/scheduler/jobs');
45
    }
46
47
    /**
48
     * @param JobEntityInterface $oJobEntity
49
     * @return bool
50
     * @throws ApiClientException
51
     */
52 10
    public function addingJob(JobEntityInterface $oJobEntity)
53
    {
54 10
        $_sTargetUrl = '';
55
56 10
        if (!$oJobEntity instanceof ChronosJobEntity) {
57
            throw new \RuntimeException('Expected ChronosJobEntity.');
58
        }
59
60 10
        if (!empty($oJobEntity->schedule) && empty($oJobEntity->parents))
61 10
        {
62 4
            $_sTargetUrl = '/scheduler/iso8601';
63 10
        } elseif (empty($oJobEntity->schedule) && !empty($oJobEntity->parents))
64
        {
65 4
            $_sTargetUrl = '/scheduler/dependency';
66 4
        }
67
68 10
        if (empty($_sTargetUrl))
69 10
        {
70 2
            throw new ApiClientException('No scheduler or dependency found. Can\'t get right target url.');
71
        }
72
73 8
        $_oResponse = $this->oHttpClient->postJsonData($_sTargetUrl, $oJobEntity);
74 8
        return ($_oResponse->getStatusCode() == 204);
75
    }
76
77
    /**
78
     * @param JobEntityInterface|ChronosJobEntity $oJobEntity
79
     * @return bool
80
     * @throws ApiClientException
81
     */
82 5
    public function updatingJob(JobEntityInterface $oJobEntity)
83
    {
84 5
        return $this->addingJob($oJobEntity);
85
    }
86
87
    /**
88
     * @param string $sJobName
89
     * @return bool
90
     */
91 2
    public function removeJob($sJobName)
92
    {
93 2
        $_oResponse = $this->oHttpClient->delete('/scheduler/job/' . $sJobName);
94 2
        return ($_oResponse->getStatusCode() == 204);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 2
    public function getJobStats($sJobName)
101
    {
102 2
        return $this->sendGetJsonRequest('/scheduler/job/stat/' . $sJobName);
103
    }
104
105
    /**
106
     * @param string $sUrl
107
     * @return array
108
     */
109 4
    private function sendGetJsonRequest($sUrl)
110
    {
111 4
        $_oResponse = $this->oHttpClient->get($sUrl);
112 4
        if (200 == $_oResponse->getStatusCode())
113 4
        {
114 2
            return $_oResponse->json();
115
        }
116
117 2
        return [];
118
    }
119
120
    /**
121
     * Returns true if the client can be connected to.
122
     * @return bool
123
     */
124
    public function ping()
125
    {
126
        try {
127
            $this->oHttpClient->get('scheduler/jobs');
128
        } catch (\Exception $e) {
129
            return false;
130
        }
131
132
        return true;
133
    }
134
}