FilesRepository   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 56
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A storeFilesChangeRate() 0 4 1
A storePhpFilesMetrics() 0 6 2
A findPhpFiles() 0 6 1
A addColumnNames() 0 12 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