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.

BridgeMarathon   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 29.41 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 72.5%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 35
loc 119
ccs 29
cts 40
cp 0.725
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A __destruct() 0 6 2
A getJobs() 0 12 3
A addJob() 8 8 2
A updateJob() 8 8 2
A removeJob() 8 8 2
A getJobList() 0 16 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:  bthapaliya
6
 * @since:   2016-12-02
7
 */
8
9
namespace Chapi\Service\JobRepository;
10
11
use Chapi\Component\Cache\CacheInterface;
12
use Chapi\Component\RemoteClients\ApiClientInterface;
13
use Chapi\Entity\Chronos\ChronosJobEntity;
14
use Chapi\Entity\JobEntityInterface;
15
use Chapi\Entity\Marathon\MarathonAppEntity;
16
use Chapi\Service\JobValidator\JobValidatorServiceInterface;
17
use Psr\Log\LoggerInterface;
18
19
class BridgeMarathon implements BridgeInterface
20
{
21
    const CACHE_TIME_JOB_LIST = 60;
22
23
    const CACHE_KEY_APP_LIST = 'marathon.app.list';
24
25
    /**
26
     * @var \Chapi\Component\RemoteClients\ApiClientInterface
27
     */
28
    private $apiClient;
29
    /**
30
     * @var JobValidatorServiceInterface
31
     */
32
    private $jobEntityValidatorService;
33
    /**
34
     * @var CacheInterface
35
     */
36
    private $cache;
37
    /**
38
     * @var LoggerInterface
39
     */
40
    private $logger;
41
42
    private $cacheHasToDelete = false;
43
44 5 View Code Duplication
    public function __construct(
45
        ApiClientInterface $apiClient,
46
        CacheInterface $cache,
47
        JobValidatorServiceInterface $jobEntityValidatorService,
48
        LoggerInterface $logger
49
    ) {
50 5
        $this->apiClient = $apiClient;
51 5
        $this->jobEntityValidatorService = $jobEntityValidatorService;
52 5
        $this->cache = $cache;
53 5
        $this->logger = $logger;
54 5
    }
55
56 5
    public function __destruct()
57
    {
58 5
        if ($this->cacheHasToDelete) {
59 2
            $this->cache->delete(self::CACHE_KEY_APP_LIST);
60
        }
61 5
    }
62
63
    /**
64
     * @return JobEntityInterface[]
65
     */
66 1
    public function getJobs()
67
    {
68 1
        $apps = [];
69 1
        $jobsList = $this->getJobList();
70
71 1
        if (!empty($jobsList)) {
72
            foreach ($jobsList as $jobData) {
73
                $apps[] = new MarathonAppEntity($jobData);
74
            }
75
        }
76 1
        return $apps;
77
    }
78
79
    /**
80
     * @param JobEntityInterface $jobEntity
81
     * @return bool
82
     */
83 2 View Code Duplication
    public function addJob(JobEntityInterface $jobEntity)
84
    {
85 2
        if ($this->apiClient->addingJob($jobEntity)) {
86 1
            $this->cacheHasToDelete = true;
87 1
            return true;
88
        }
89 1
        return false;
90
    }
91
92
    /**
93
     * @param JobEntityInterface $jobEntity
94
     * @return bool
95
     */
96 View Code Duplication
    public function updateJob(JobEntityInterface $jobEntity)
97
    {
98
        if ($this->apiClient->updatingJob($jobEntity)) {
99
            $this->cacheHasToDelete = true;
100
            return true;
101
        }
102
        return false;
103
    }
104
105
    /**
106
     * @param JobEntityInterface $jobEntity
107
     * @return bool
108
     */
109 2 View Code Duplication
    public function removeJob(JobEntityInterface $jobEntity)
110
    {
111 2
        if ($this->apiClient->removeJob($jobEntity->getKey())) {
112 1
            $this->cacheHasToDelete = true;
113 1
            return true;
114
        }
115 1
        return false;
116
    }
117
118
    /**
119
     * @return array|mixed
120
     */
121 1
    private function getJobList()
122
    {
123 1
        $result = $this->cache->get(self::CACHE_KEY_APP_LIST);
124
125 1
        if (is_array($result)) {
126 1
            return $result;
127
        }
128
129
        $result = $this->apiClient->listingJobs();
130
131
        if (!empty($result['apps'])) {
132
            $this->cache->set(self::CACHE_KEY_APP_LIST, $result['apps'], self::CACHE_TIME_JOB_LIST);
133
        }
134
135
        return $result['apps'];
136
    }
137
}
138