Completed
Push — master ( 5bda77...cb2c73 )
by Aleh
10s
created

Repository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 7
c 3
b 0
f 3
lcom 1
cbo 3
dl 0
loc 42
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A findByPath() 0 7 2
A read() 0 9 2
A loadCoreIndex() 0 11 2
1
<?php
2
3
namespace Padawan\Framework\Project;
4
5
6
use Padawan\Framework\IO\Reader;
7
use Padawan\Domain\ProjectRepository;
8
use Padawan\Domain\Core\Project;
9
use Padawan\Domain\Core\Index;
10
11
/**
12
 * Class Repository
13
 */
14
class Repository implements ProjectRepository
15
{
16
    public function __construct(Persister $persister)
17
    {
18
        $this->persister = $persister;
19
        $this->pool = [];
20
        $this->loadCoreIndex();
21
    }
22
    public function findByPath($path)
23
    {
24
        if (!array_key_exists($path, $this->pool)) {
25
            $this->pool[$path] = $this->read($path);
26
        }
27
        return $this->pool[$path];
28
    }
29
30
    private function read($path)
31
    {
32
        $project = $this->persister->load($path);
33
        if (!empty($project)) {
34
            return $project;
35
        } else {
36
            return new Project(new Index, $path);
37
        }
38
    }
39
40
    private function loadCoreIndex()
41
    {
42
        if (self::$coreIndex) {
43
            return;
44
        }
45
        self::$coreIndex = $this->read(STUBS_DIR)->getIndex();
46
        $indexClass = new \ReflectionClass(Index::class);
47
        $coreIndexProperty = $indexClass->getProperty("coreIndex");
48
        $coreIndexProperty->setAccessible(true);
49
        $coreIndexProperty->setValue(self::$coreIndex);
50
    }
51
52
    private static $coreIndex;
53
    private $pool;
54
    private $persister;
55
}
56