Completed
Push — master ( 81db43...f030be )
by Aleh
12s
created

ProjectRepository::loadCoreIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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