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
Push — master ( 42287f...fba610 )
by Andy
03:33
created

BridgeChronos   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 180
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 6
dl 20
loc 180
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A __destruct() 0 6 2
A getJobs() 0 14 3
A addJob() 0 4 1
A updateJob() 0 4 1
A removeJob() 9 9 2
A hasValidate() 0 22 2
A getJobList() 0 17 3
A hasAddOrUpdateJob() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2015-07-28
7
 *
8
 */
9
10
namespace Chapi\Service\JobRepository;
11
12
use Chapi\Component\Cache\CacheInterface;
13
use Chapi\Component\RemoteClients\ApiClientInterface;
14
use Chapi\Entity\Chronos\ChronosJobEntity;
15
use Chapi\Entity\JobEntityInterface;
16
use Chapi\Service\JobValidator\JobValidatorServiceInterface;
17
use Psr\Log\LoggerInterface;
18
19
class BridgeChronos implements BridgeInterface
20
{
21
    const CACHE_TIME_JOB_LIST = 60;
22
23
    const CACHE_KEY_JOB_LIST = 'jobs.list';
24
25
    const API_CALL_ADD = 'addingJob';
26
27
    const API_CALL_UPDATE = 'updatingJob';
28
29
    /**
30
     * @var ApiClientInterface
31
     */
32
    private $apiClient;
33
34
    /**
35
     * @var CacheInterface
36
     */
37
    private $cache;
38
39
    /**
40
     * @var JobValidatorServiceInterface
41
     */
42
    private $jobEntityValidatorService;
43
44
    /**
45
     * @var bool
46
     */
47
    private $cacheHasToDelete = false;
48
49
    /**
50
     * @var LoggerInterface
51
     */
52
    private $logger;
53
54
    /**
55
     * @param \Chapi\Component\RemoteClients\ApiClientInterface $apiClient
56
     * @param CacheInterface $cache
57
     * @param JobValidatorServiceInterface $jobEntityValidatorService
58
     * @param LoggerInterface $logger
59
     */
60 8 View Code Duplication
    public function __construct(
61
        ApiClientInterface $apiClient,
62
        CacheInterface $cache,
63
        JobValidatorServiceInterface $jobEntityValidatorService,
64
        LoggerInterface $logger
65
    ) {
66 8
        $this->apiClient = $apiClient;
67 8
        $this->cache = $cache;
68 8
        $this->jobEntityValidatorService = $jobEntityValidatorService;
69 8
        $this->logger = $logger;
70 8
    }
71
72
    /**
73
     * delete cache job.list if a job was removed
74
     */
75 8
    public function __destruct()
76
    {
77 8
        if ($this->cacheHasToDelete) {
78 3
            $this->cache->delete(self::CACHE_KEY_JOB_LIST);
79
        }
80 8
    }
81
82
83
    /**
84
     * @return ChronosJobEntity[]
85
     */
86 2
    public function getJobs()
87
    {
88 2
        $return = [];
89 2
        $jobList = $this->getJobList();
90
91 2
        if (!empty($jobList)) {
92
            // prepare return value
93 2
            foreach ($jobList as $jobData) {
94 2
                $return[] = new ChronosJobEntity($jobData);
95
            }
96
        }
97
98 2
        return $return;
99
    }
100
101
    /**
102
     * @param ChronosJobEntity|JobEntityInterface $jobEntity
103
     * @return bool
104
     */
105 2
    public function addJob(JobEntityInterface $jobEntity)
106
    {
107 2
        return $this->hasAddOrUpdateJob(self::API_CALL_ADD, $jobEntity);
108
    }
109
110
    /**
111
     * @param ChronosJobEntity|JobEntityInterface $jobEntity
112
     * @return bool
113
     */
114 2
    public function updateJob(JobEntityInterface $jobEntity)
115
    {
116 2
        return $this->hasAddOrUpdateJob(self::API_CALL_UPDATE, $jobEntity);
117
    }
118
119
120
    /**
121
     * @param ChronosJobEntity|JobEntityInterface $jobEntity
122
     * @return bool
123
     */
124 2 View Code Duplication
    public function removeJob(JobEntityInterface $jobEntity)
125
    {
126 2
        if ($this->apiClient->removeJob($jobEntity->getKey())) {
127 1
            $this->cacheHasToDelete = true;
128 1
            return true;
129
        }
130
131 2
        return false;
132
    }
133
134
    /**
135
     * @param JobEntityInterface $jobEntity
136
     * @return bool
137
     */
138 4
    private function hasValidate(JobEntityInterface $jobEntity)
139
    {
140 4
        $invalidProperties = $this->jobEntityValidatorService->getInvalidProperties($jobEntity);
141 4
        if (empty($invalidProperties)) {
142 2
            return true;
143
        }
144
145 2
        $this->logger->warning(
146
            sprintf(
147 2
                "Can't update job '%s'",
148 2
                $jobEntity->getKey()
149
            )
150
        );
151 2
        $this->logger->warning(
152
            sprintf(
153 2
                "The following job entity properties are not valid:\n%s",
154 2
                implode(', ', $invalidProperties)
155
            )
156
        );
157
158 2
        return false;
159
    }
160
161
    /**
162
     * @return array
163
     */
164 2
    private function getJobList()
165
    {
166 2
        $result = $this->cache->get(self::CACHE_KEY_JOB_LIST);
167
168 2
        if (is_array($result)) {
169
            // return list from cache
170 1
            return $result;
171
        }
172
173 1
        $result = $this->apiClient->listingJobs();
174 1
        if (!empty($result)) {
175
            // set result to cache
176 1
            $this->cache->set(self::CACHE_KEY_JOB_LIST, $result, self::CACHE_TIME_JOB_LIST);
177
        }
178
179 1
        return $result;
180
    }
181
182
    /**
183
     * @param string $apiMethod
184
     * @param JobEntityInterface $jobEntity
185
     * @return bool
186
     */
187 4
    private function hasAddOrUpdateJob($apiMethod, JobEntityInterface $jobEntity)
188
    {
189 4
        if ($this->hasValidate($jobEntity)) {
190 2
            if ($this->apiClient->{$apiMethod}($jobEntity)) {
191 2
                $this->cacheHasToDelete = true;
192 2
                return true;
193
            }
194
        }
195
196 2
        return false;
197
    }
198
}
199