Passed
Branch master (8cb5b2)
by Christopher
04:37
created

Mapper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 89.66%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 81
ccs 26
cts 29
cp 0.8966
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMappingFiles() 0 7 1
A run() 0 27 5
A isMappingAlreadyRun() 0 7 2
A __construct() 0 8 1
1
<?php
2
namespace Triadev\Leopard\Business\Mapping;
3
4
use Illuminate\Filesystem\Filesystem;
5
use Triadev\Leopard\Contract\Repository\MappingLogRepositoryContract;
6
7
class Mapper
8
{
9
    /** @var Filesystem */
10
    private $filesystem;
11
    
12
    /** @var MappingLogRepositoryContract */
13
    private $mappingLogRepository;
14
    
15
    /** @var array */
16
    private $mappingLogs;
17
    
18
    /**
19
     * Mapper constructor.
20
     * @param Filesystem $filesystem
21
     * @param MappingLogRepositoryContract $mappingLogRepository
22
     */
23 8
    public function __construct(Filesystem $filesystem, MappingLogRepositoryContract $mappingLogRepository)
24
    {
25 8
        $this->filesystem = $filesystem;
26 8
        $this->mappingLogRepository = $mappingLogRepository;
27
    
28
        $this->mappingLogs = array_map(function ($log) {
29
            return $log['mapping'];
30 8
        }, $this->mappingLogRepository->all());
31 8
    }
32
    
33
    /**
34
     * Run mappings
35
     *
36
     * @param string $path
37
     * @param string|null $index
38
     * @param string|null $type
39
     *
40
     * @throws \Throwable
41
     */
42 8
    public function run(string $path, ?string $index = null, ?string $type = null)
43
    {
44 8
        $mappingFiles = $this->getMappingFiles($path);
45
        
46 8
        sort($mappingFiles);
47
        
48 8
        foreach ($mappingFiles as $mappingFile) {
49 8
            if ($this->isMappingAlreadyRun($mappingFile)) {
50
                continue;
51
            }
52
            
53 8
            $this->filesystem->requireOnce($path . DIRECTORY_SEPARATOR . $mappingFile . '.php');
54
            
55
            /** @var Mapping $mapping */
56 8
            $mapping = new $mappingFile();
57
            
58 8
            if ($index) {
59 1
                $mapping->setDocumentIndex($index);
60
            }
61
            
62 8
            if ($type) {
63 1
                $mapping->setDocumentType($type);
64
            }
65
            
66 8
            $mapping->map();
67
            
68 8
            $this->mappingLogRepository->add($mappingFile);
69
        }
70 8
    }
71
    
72 8
    private function getMappingFiles(string $path): array
73
    {
74 8
        $files = $this->filesystem->glob($path . DIRECTORY_SEPARATOR . '*.php');
75
        
76
        return array_map(function (string $file) {
77 8
            return str_replace('.php', '', basename($file));
78 8
        }, $files);
79
    }
80
    
81 8
    private function isMappingAlreadyRun(string $mapping) : bool
82
    {
83 8
        if (in_array($mapping, $this->mappingLogs)) {
84
            return true;
85
        }
86
        
87 8
        return false;
88
    }
89
}
90