AspectBind   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A bind() 0 15 3
A makeCacheDir() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2015-2020 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelAspect;
21
22
use Illuminate\Filesystem\Filesystem;
23
use Ray\Aop\Bind;
24
25
use function str_replace;
26
use function serialize;
27
use function unserialize;
28
use function file_get_contents;
29
30
/**
31
 * Class AspectBind
32
 */
33
class AspectBind
34
{
35
    /** @var bool */
36
    protected $cacheable;
37
38
    /** @var string */
39
    protected $path;
40
41
    /** @var Filesystem */
42
    protected $filesystem;
43
44
    /** @var string */
45
    protected $extension = '.cached.php';
46
47
    /**
48
     * AspectBind constructor.
49
     *
50
     * @param Filesystem $filesystem
51
     * @param string     $path
52
     * @param bool       $cacheable
53
     */
54
    public function __construct(
55
        Filesystem $filesystem,
56
        string $path,
57
        bool $cacheable = false
58
    ) {
59
        $this->filesystem = $filesystem;
60
        $this->cacheable = $cacheable;
61
        $this->path = $path;
62
    }
63
64
    /**
65
     * @param string $class
66
     * @param array $pointcuts
67
     * @return mixed|\Ray\Aop\BindInterface
68
     * @throws \Doctrine\Common\Annotations\AnnotationException
69
     * @throws \ReflectionException
70
     */
71
    public function bind(string $class, array $pointcuts)
72
    {
73
        if (!$this->cacheable) {
74
            return (new Bind)->bind($class, $pointcuts);
75
        }
76
        $className = str_replace("\\", "_", $class);
77
        $filePath = $this->path . "/{$className}" . $this->extension;
78
        if (!$this->filesystem->exists($filePath)) {
79
            $this->makeCacheDir($this->path);
80
            $bind = (new Bind)->bind($class, $pointcuts);
81
            $this->filesystem->put($filePath, serialize($bind));
82
        }
83
84
        return unserialize(file_get_contents($filePath));
85
    }
86
87
    /**
88
     * @param string $path
89
     */
90
    private function makeCacheDir(string $path): void
91
    {
92
        if (!$this->filesystem->exists($path)) {
93
            $this->filesystem->makeDirectory($path, 0777, true);
94
        }
95
    }
96
}
97