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 (#52)
by Marc
02:52
created

BridgeFileSystem   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.79%

Importance

Changes 6
Bugs 1 Features 4
Metric Value
wmc 26
c 6
b 1
f 4
lcom 1
cbo 4
dl 0
loc 253
ccs 91
cts 95
cp 0.9579
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getJobs() 0 10 2
A addJob() 0 13 2
A updateJob() 0 7 1
A removeJob() 0 7 1
A generateJobFilePath() 0 9 1
B getJobFilesFromFileSystem() 0 22 6
A setJobFileToMap() 0 13 2
A getJobFileFromMap() 0 9 2
A hasUnsetJobFileFromMap() 0 12 3
B loadJobsFromFileContent() 0 37 4
A hasDumpFile() 0 9 1
1
<?php
2
/**
3
     * @package: chapi
4
     *
5
     * @author:  msiebeneicher
6
     * @since:   2015-07-29
7
     *
8
     */
9
10
namespace Chapi\Service\JobRepository;
11
12
use Chapi\Component\Cache\CacheInterface;
13
use Chapi\Entity\Chronos\JobEntity;
14
use Chapi\Exception\JobLoadException;
15
use Symfony\Component\Filesystem\Filesystem;
16
use Webmozart\Glob\Glob;
17
18
class BridgeFileSystem implements BridgeInterface
19
{
20
    /**
21
     * @var Filesystem
22
     */
23
    private $oFileSystemService;
24
25
    /**
26
     * @var CacheInterface
27
     */
28
    private $oCache;
29
30
    /**
31
     * @var string
32
     */
33
    private $sRepositoryDir = '';
34
35
    /**
36
     * @var string[]
37
     */
38
    private $aDirectorySeparators = ['.', ':', '-', '\\'];
39
40
    /**
41
     * @var array
42
     */
43
    private $aJobFileMap = [];
44
45
    /**
46
     * @param Filesystem $oFileSystemService
47
     * @param CacheInterface $oCache
48
     * @param string $sRepositoryDir
49
     */
50 5
    public function __construct(
51
        Filesystem $oFileSystemService,
52
        CacheInterface $oCache,
53
        $sRepositoryDir
54
    )
55
    {
56 5
        $this->oFileSystemService = $oFileSystemService;
57 5
        $this->oCache = $oCache;
58 5
        $this->sRepositoryDir = $sRepositoryDir;
59 5
    }
60
61
    /**
62
     * @return JobEntity[]
63
     */
64 4
    public function getJobs()
65
    {
66 4
        if (empty($this->aJobFileMap))
67 4
        {
68 4
            $_aJobFiles = $this->getJobFilesFromFileSystem($this->sRepositoryDir);
69 4
            return $this->loadJobsFromFileContent($_aJobFiles, true);
70
        }
71
72 1
        return $this->loadJobsFromFileContent($this->aJobFileMap, false);
73
    }
74
75
    /**
76
     * @param JobEntity $oJobEntity
77
     * @return bool
78
     */
79 1
    public function addJob(JobEntity $oJobEntity)
80
    {
81
        // generate job file path by name
82 1
        $_sJobFile = $this->generateJobFilePath($oJobEntity);
83
84 1
        if ($this->hasDumpFile($_sJobFile, $oJobEntity))
85 1
        {
86 1
            $this->setJobFileToMap($oJobEntity->name, $_sJobFile);
87 1
            return true;
88
        }
89
90
        return false;
91
    }
92
93
    /**
94
     * @param JobEntity $oJobEntity
95
     * @return bool
96
     */
97 1
    public function updateJob(JobEntity $oJobEntity)
98
    {
99 1
        return $this->hasDumpFile(
100 1
            $this->getJobFileFromMap($oJobEntity->name),
101
            $oJobEntity
102 1
        );
103
    }
104
105
    /**
106
     * @param JobEntity $oJobEntity
107
     * @return bool
108
     */
109 1
    public function removeJob(JobEntity $oJobEntity)
110
    {
111 1
        $_sJobFile = $this->getJobFileFromMap($oJobEntity->name);
112 1
        $this->oFileSystemService->remove($_sJobFile);
113
114 1
        return $this->hasUnsetJobFileFromMap($oJobEntity->name, $_sJobFile);
115
    }
116
117
    /**
118
     * @param JobEntity $oJobEntity
119
     * @return string
120
     */
121 1
    private function generateJobFilePath(JobEntity $oJobEntity)
122
    {
123 1
        $_sJobPath = str_replace(
124 1
            $this->aDirectorySeparators,
125 1
            DIRECTORY_SEPARATOR,
126 1
            $oJobEntity->name
127 1
        );
128 1
        return $this->sRepositoryDir . DIRECTORY_SEPARATOR . $_sJobPath . '.json';
129
    }
130
131
    /**
132
     * @param string $sPath
133
     * @param array $aJobFiles
134
     * @return array
135
     */
136 4
    private function getJobFilesFromFileSystem($sPath, array &$aJobFiles = [])
137
    {
138 4
        if (!is_dir($sPath))
139 4
        {
140
            throw new \RuntimeException(sprintf('Path "%s" is not valid', $sPath));
141
        }
142
143 4
        $_aTemp = Glob::glob(rtrim($sPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*');
144
145 4
        foreach ($_aTemp as $_sPath)
146
        {
147 3
            if (is_file($_sPath) && preg_match('~\.json~i', $_sPath))
148 3
            {
149 3
                $aJobFiles[] = $_sPath;
150 3
            } elseif (is_dir($_sPath))
151
            {
152 3
                $this->getJobFilesFromFileSystem($_sPath, $aJobFiles);
153 3
            }
154 4
        }
155
156 4
        return $aJobFiles;
157
    }
158
159
    /**
160
     * @param string $sJobName
161
     * @param string $sJobFile
162
     * @throws JobLoadException
163
     */
164 3
    private function setJobFileToMap($sJobName, $sJobFile)
165
    {
166
        // set path to job file map
167 3
        if (isset($this->aJobFileMap[$sJobName]))
168 3
        {
169 1
            throw new JobLoadException(
170 1
                sprintf('The jobname "%s" already exists. Jobnames have to be unique - Please check your local jobfiles for duplicate entries.', $sJobName),
171
                JobLoadException::ERROR_CODE_DUPLICATE_JOB_ID
172 1
            );
173
        }
174
175 3
        $this->aJobFileMap[$sJobName] = $sJobFile;
176 3
    }
177
178
    /**
179
     * @param string $sJobName
180
     * @return string
181
     * @throws \RuntimeException
182
     */
183 1
    private function getJobFileFromMap($sJobName)
184
    {
185 1
        if (!isset($this->aJobFileMap[$sJobName]))
186 1
        {
187
            throw new \RuntimeException(sprintf('Can\'t find file for job "%s"', $sJobName));
188
        }
189
190 1
        return $this->aJobFileMap[$sJobName];
191
    }
192
193
    /**
194
     * @param string $sJobName
195
     * @param string $sJobFile
196
     * @return bool
197
     * @throws \RuntimeException
198
     */
199 1
    private function hasUnsetJobFileFromMap($sJobName, $sJobFile = '')
200
    {
201 1
        $_sJobFile = (!empty($sJobFile)) ? $sJobFile : $this->getJobFileFromMap($sJobName);
202 1
        if (file_exists($_sJobFile))
203 1
        {
204
            throw new \RuntimeException(sprintf('Job file "%s" for job "%s" still exists.', $_sJobFile, $sJobName));
205
        }
206
207
        // unset path from job file map
208 1
        unset($this->aJobFileMap[$sJobName]);
209 1
        return true;
210
    }
211
212
    /**
213
     * @param array $aJobFiles
214
     * @param bool $bSetToFileMap
215
     * @return JobEntity[]
216
     * @throws JobLoadException
217
     */
218 4
    private function loadJobsFromFileContent(array $aJobFiles, $bSetToFileMap)
219
    {
220 4
        $_aJobs = [];
221
222 4
        foreach ($aJobFiles as $_sJobFilePath)
223
        {
224
            // remove comment blocks
225 4
            $_aTemp = json_decode(
226 4
                preg_replace(
227 4
                    '~\/\*(.*?)\*\/~mis',
228 4
                    '',
229 4
                    file_get_contents($_sJobFilePath)
230 4
                )
231 4
            );
232
233
            if ($_aTemp)
234 4
            {
235 3
                $_oJobEntity = new JobEntity($_aTemp);
236 3
                $_aJobs[] = $_oJobEntity;
237
238
                if ($bSetToFileMap)
239 3
                {
240
                    // set path to job file map
241 2
                    $this->setJobFileToMap($_oJobEntity->name, $_sJobFilePath);
242 2
                }
243 3
            }
244
            else
245
            {
246 1
                throw new JobLoadException(
247 1
                    sprintf('Unable to load json job data from "%s". Please check if the json is valid.', $_sJobFilePath),
248
                    JobLoadException::ERROR_CODE_NO_VALID_JSON
249 1
                );
250
            }
251 3
        }
252
253 2
        return $_aJobs;
254
    }
255
256
    /**
257
     * @param string $sJobFile
258
     * @param JobEntity $oJobEntity
259
     * @return bool
260
     */
261 1
    private function hasDumpFile($sJobFile, JobEntity $oJobEntity)
262
    {
263 1
        $this->oFileSystemService->dumpFile(
264 1
            $sJobFile,
265 1
            json_encode($oJobEntity, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
266 1
        );
267
268 1
        return (file_exists($sJobFile));
269
    }
270
}