Completed
Pull Request — master (#81)
by
unknown
03:21 queued 33s
created

Persister::checkForPadawanDir()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
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 $this->fs->file(
30
            $this->getProjectIndexFilePath($project->getRootFolder())
31
        )->putContents($this->serialize($project));
32
    }
33
34
    public function saveNow(Project $project)
35
    {
36
        $this->checkForPadawanDir($project->getRootFolder());
37
        return file_put_contents(
38
            $this->getProjectIndexFilePath($project->getRootFolder()),
39
            $this->serialize($project)
40
        );
41
    }
42
43
    public function load($rootDir)
44
    {
45
        try {
46
            $project = $this->unserialize(
47
                $this->readFromFile($this->getProjectIndexFilePath($rootDir))
48
            );
49
            if ($project instanceof __PHP_Incomplete_Class
50
                || $project->getIndex() instanceof __PHP_Incomplete_Class
51
            ) {
52
                return;
53
            }
54
            return $project;
55
        } catch (\Exception $e) {
56
            return;
57
        }
58
    }
59
60
    private function unserialize($rawProject)
61
    {
62
        if (function_exists('igbinary_serialize')) {
63
            $project = @igbinary_unserialize($rawProject);
64
            if (!empty($project)) {
65
                return $project;
66
            }
67
            // fallback to native unserialize function
68
        }
69
        return unserialize($rawProject);
70
    }
71
72
    private function getProjectIndexFilePath($rootDir)
73
    {
74
        return $this->path->join([
75
            $rootDir,
76
            self::PADAWAN_DIR,
77
            self::INDEX_FILE
78
        ]);
79
    }
80
81
    private function serialize(Project $project)
82
    {
83
        if (function_exists('igbinary_serialize')) {
84
            return igbinary_serialize($project);
85
        }
86
87
        return serialize($project);
88
    }
89
90
    private function readFromFile($filename)
91
    {
92
        return $this->path->read($filename);
93
    }
94
95
    private function checkForPadawanDir($dir)
96
    {
97
        $padawanDir = $this->path->join([$dir, self::PADAWAN_DIR]);
98
        if ($this->path->isDir($padawanDir)) {
99
            return;
100
        }
101
        if ($this->path->exists($padawanDir)) {
102
            $this->path->remove($padawanDir);
103
        }
104
        $this->path->create($padawanDir, true);
105
    }
106
107
    /**
108
     *
109
     * @var PathResolver
110
     */
111
    private $path;
112
113
    /**
114
     * @var Filesystem
115
     */
116
    private $fs;
117
}
118