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

Persister::getProjectIndexFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Padawan\Framework\Project;
4
5
6
use Padawan\Framework\Utils\PathResolver;
7
use Padawan\Domain\Core\Project;
8
use \__PHP_Incomplete_Class;
9
use Amp\File;
10
11
/**
12
 * Class Persister
13
 */
14
class Persister
15
{
16
    const INDEX_FILE = ".padawan/project";
17
18
    public function __construct(PathResolver $path)
19
    {
20
        $this->path = $path;
21
    }
22
23
    public function save(Project $project)
24
    {
25
        return File\put($this->getProjectIndexFilePath($project->getRootFolder()), $this->serialize($project));
26
    }
27
28
    public function load($rootDir)
29
    {
30
        try {
31
            $project = $this->unserialize(
32
                $this->readFromFile($this->getProjectIndexFilePath($rootDir))
33
            );
34
            if ($project instanceof __PHP_Incomplete_Class) {
35
                return;
36
            }
37
            return $project;
38
        } catch (\Exception $e) {
39
            return;
40
        }
41
    }
42
    protected function unserialize($rawProject) {
43
        return unserialize($rawProject);
44
    }
45
46
    private function getProjectIndexFilePath($rootDir)
47
    {
48
        return $this->path->join([
49
            $rootDir,
50
            self::INDEX_FILE
51
        ]);
52
    }
53
54
    private function serialize(Project $project)
55
    {
56
        return serialize($project);
57
    }
58
59
    private function readFromFile($filename) {
60
        return $this->path->read($filename);
61
    }
62
63
    /**
64
     *
65
     * @var PathResolver
66
     */
67
    private $path;
68
}
69