Persister::readFromFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Padawan\Framework\Domain\Project;
4
5
6
use Padawan\Domain\Project;
7
use __PHP_Incomplete_Class;
8
use React\EventLoop\LoopInterface;
9
use React\Filesystem\Filesystem;
10
use Padawan\Framework\Utils\PathResolver;
11
12
/**
13
 * Class Persister
14
 */
15
class Persister
16
{
17
    const PADAWAN_DIR = ".padawan";
18
    const INDEX_FILE = "project";
19
20
    public function __construct(PathResolver $path, LoopInterface $loop)
21
    {
22
        $this->path = $path;
23
        $this->fs = Filesystem::create($loop);
24
    }
25
26
    public function save(Project $project)
27
    {
28
        $this->checkForPadawanDir($project->getRootFolder());
29
        return file_put_contents(
30
            $this->getProjectIndexFilePath($project->getRootFolder()),
31
            $this->serialize($project)
32
        );
33
    }
34
35
    public function load($rootDir)
36
    {
37
        try {
38
            $project = $this->unserialize(
39
                $this->readFromFile($this->getProjectIndexFilePath($rootDir))
40
            );
41
            if ($project instanceof __PHP_Incomplete_Class
42
                || $project->getIndex() instanceof __PHP_Incomplete_Class
43
            ) {
44
                return;
45
            }
46
            return $project;
47
        } catch (\Exception $e) {
48
            return;
49
        }
50
    }
51
52
    private function unserialize($rawProject)
53
    {
54
        if (function_exists('igbinary_serialize')) {
55
            $project = @igbinary_unserialize($rawProject);
56
            if (!empty($project)) {
57
                return $project;
58
            }
59
            // fallback to native unserialize function
60
        }
61
        return unserialize($rawProject);
62
    }
63
64
    private function getProjectIndexFilePath($rootDir)
65
    {
66
        return $this->path->join([
67
            $rootDir,
68
            self::PADAWAN_DIR,
69
            self::INDEX_FILE
70
        ]);
71
    }
72
73
    private function serialize(Project $project)
74
    {
75
        if (function_exists('igbinary_serialize')) {
76
            return igbinary_serialize($project);
77
        }
78
79
        return serialize($project);
80
    }
81
82
    private function readFromFile($filename)
83
    {
84
        return $this->path->read($filename);
85
    }
86
87
    private function checkForPadawanDir($dir)
88
    {
89
        $padawanDir = $this->path->join([$dir, self::PADAWAN_DIR]);
90
        if ($this->path->isDir($padawanDir)) {
91
            return;
92
        }
93
        if ($this->path->exists($padawanDir)) {
94
            $this->path->remove($padawanDir);
95
        }
96
        $this->path->create($padawanDir, true);
97
    }
98
99
    /**
100
     *
101
     * @var PathResolver
102
     */
103
    private $path;
104
105
    /**
106
     * @var Filesystem
107
     */
108
    private $fs;
109
}
110