Passed
Branch master (634da9)
by refat
03:15
created

File::processSharing()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
use Exception;
6
7
class File
8
{
9
    /**
10
     * Root path
11
     *
12
     * @var string
13
     */
14
    private $root;
15
16
    /**
17
     * Container
18
     *
19
     * @var array
20
     */
21
    private $container = [];
22
23
    /**
24
     * Constructor
25
     *
26
     * @param string $root
27
     */
28
    public function __construct($root)
29
    {
30
        $this->root = $root;
31
    }
32
33
    /**
34
     * Root
35
     *
36
     * @return string $root
37
     */
38
    public function root()
39
    {
40
        return $this->root;
41
    }
42
43
    /**
44
     * Add the given path file to the container
45
     *
46
     * @param string $key
47
     * @param mixed $value
48
     * @return void
49
     */
50
    private function share($key, $value)
51
    {
52
        $this->container[$key] = $value;
53
    }
54
55
    /**
56
     * Check if the given path file exists in the container
57
     *
58
     * @param string $key
59
     * @return bool
60
     */
61
    private function isSharing($key)
62
    {
63
        return isset($this->container[$key]);
64
    }
65
66
    /**
67
     * Determine if the given file path exists
68
     *
69
     * @param string $file
70
     * @return bool
71
     */
72
    public function exists($file)
73
    {
74
        return file_exists($file);
75
    }
76
77
    /**
78
     * Require the given file
79
     *
80
     * @param string $path
81
     * @return mixed
82
     */
83
    public function call($path)
84
    {
85
        return $this->processSharing($path, 'file');
86
    }
87
88
    /**
89
     * Get file content
90
     *
91
     * @param string $path
92
     * @return mixed
93
     */
94
    public function fileContent($path)
95
    {
96
        return $this->processSharing($path, 'content');
97
    }
98
99
    /**
100
     * After getting the $path, check if the file or the conent of the file
101
     * is already exists:
102
     * -if so: just return it from the container.
103
     * -if not: check if the file exits:
104
     *  - if so: share it to the container. the $path as the key plus :content or :file
105
     *      to avoid conflict bewtween the keys.
106
     *      :file: it's mean the file
107
     *      :content: it's mean the content of the file
108
     *  - if not: theow an Exception.
109
     *
110
     * @param string $path
111
     * @param string $share
112
     * @return mixed
113
     */
114
    private function processSharing($path, $share)
115
    {
116
        $path = $this->fullPath($path);
117
118
        if (!$this->isSharing($path . ':' . $share)) {
119
            if ($this->exists($path)) {
120
                $this->share($path . ':' . $share, ($share === 'content') ? file_get_contents($path) : require $path);
121
            } else {
122
                throw new Exception("$path is not found");
123
            }
124
        }
125
126
        return $this->container[$path . ':' . $share];
127
    }
128
129
    /**
130
     * Generate full path to the given path
131
     *
132
     * @param string $path
133
     * @return string
134
     */
135
    public function fullPath($path)
136
    {
137
        return $this->root . DS . str_replace(['/', '\\'], DS, $path);
138
    }
139
}
140