Passed
Push — master ( 9251f2...ca8fc4 )
by Herberto
03:14
created

FilesRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Hgraca\Phorensic\SharedKernel\Repository;
4
5
use Hgraca\Phorensic\SharedKernel\Port\Database\DatabaseClientInterface;
6
use Hgraca\Phorensic\SharedKernel\Port\FileSystem\Adapter\FileSystem\FileSystemAdapter;
7
use Hgraca\Phorensic\SharedKernel\Port\FileSystem\FileSystemInterface;
8
9
final class FilesRepository implements FilesRepositoryInterface
10
{
11
    const TBL_FILES = 'files';
12
    const TBL_FILES_COL_PATH = 'path';
13
    const TBL_FILES_COL_TYPE = 'type';
14
    const TBL_FILES_COL_COMMITS = 'commits';
15
    const TBL_FILES_COL_ACTIVE_DAYS = 'active_days';
16
17
    /**
18
     * @var DatabaseClientInterface
19
     */
20
    private $dbClient;
21
22
    /**
23
     * @var FileSystemInterface
24
     */
25
    private $fileSystem;
26
27
    public function __construct(DatabaseClientInterface $dbClient, FileSystemInterface $fileSystem = null)
28
    {
29
        $this->dbClient = $dbClient;
30
        $this->fileSystem = $fileSystem ?? new FileSystemAdapter();
31
    }
32
33
    public function storeFilesChangeRate(array $data)
34
    {
35
        $this->dbClient->create(self::TBL_FILES, $this->addColumnNames($data));
36
    }
37
38
    public function storePhpFilesMetrics(array $data)
39
    {
40
        foreach ($data as $filePath => $metrics) {
41
            $this->dbClient->update(self::TBL_FILES, $metrics, ['path' => $filePath]);
42
        }
43
    }
44
45
    public function findPhpFiles(): array
46
    {
47
        $fileList = $this->dbClient->read(self::TBL_FILES, [self::TBL_FILES_COL_TYPE => 'php'], [], null);
48
49
        return array_column($fileList, self::TBL_FILES_COL_PATH);
50
    }
51
52
    private function addColumnNames(array $data): array
53
    {
54
        $dataWithColumnNames = [];
55
        foreach ($data as $file) {
56
            $dataWithColumnNames[$file[0]][self::TBL_FILES_COL_PATH] = $file[0];
57
            $dataWithColumnNames[$file[0]][self::TBL_FILES_COL_TYPE] = $this->fileSystem->getExtension($file[0]);
58
            $dataWithColumnNames[$file[0]][self::TBL_FILES_COL_COMMITS] = $file[1];
59
            $dataWithColumnNames[$file[0]][self::TBL_FILES_COL_ACTIVE_DAYS] = $file[2];
60
        }
61
62
        return $dataWithColumnNames;
63
    }
64
}
65