Completed
Pull Request — master (#22)
by MoshiMoshi
06:45
created

RestorablePhpFileCache::setBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the ConfigCacheBundle package.
5
 *
6
 * Copyright (c) 2015-2016 Yahoo Japan Corporation
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace YahooJapan\ConfigCacheBundle\ConfigCache;
13
14
use Doctrine\Common\Cache\PhpFileCache;
15
16
/**
17
 * RestorablePhpFileCache restores by re-creating the caches to the temporary directory or cache directory.
18
 */
19
class RestorablePhpFileCache extends PhpFileCache
20
{
21
    const TAG_RESTORABLE_CACHE = 'config_cache.restorable';
22
23
    protected $builder;
24
    protected $restoringDirectory;
25
26
    /**
27
     * Saves the cache to the temporary directory.
28
     *
29
     * @param string $id
30
     */
31 7 View Code Duplication
    public function saveToTemp($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33 7
        if ($this->contains($id)) {
34 6
            $data = $this->fetch($id);
35 6
            $this->prepareTemporaryDirectory();
36 6
            $this->save($id, $data);
37 6
            $this->restoreDirectory();
38 6
        }
39 7
    }
40
41
    /**
42
     * Restores the cache to the cache directory.
43
     *
44
     * @param string $id
45
     */
46 4 View Code Duplication
    public function restore($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 4
        $this->prepareTemporaryDirectory();
49 4
        if ($this->contains($id)) {
50 3
            $data = $this->fetch($id);
51 3
            $this->restoreDirectory();
52 3
            $this->save($id, $data);
53 3
        } else {
54 1
            $this->restoreDirectory();
55
        }
56 4
    }
57
58
    /**
59
     * Sets a SaveAreaBuilder.
60
     *
61
     * @param SaveAreaBuilder $builder
62
     *
63
     * @return RestorablePhpFileCache
64
     */
65 13
    public function setBuilder(SaveAreaBuilder $builder)
66
    {
67 13
        $this->builder = $builder;
68
69 13
        return $this;
70
    }
71
72
    /**
73
     * Sets a directory.
74
     *
75
     * @param string $directory
76
     *
77
     * @return RestorablePhpFileCache
78
     */
79 11
    protected function setDirectory($directory)
80
    {
81 11
        $this->directory = realpath($directory);
82 11
        $this->directoryStringLength = strlen($this->directory);
0 ignored issues
show
Bug introduced by
The property directoryStringLength cannot be accessed from this context as it is declared private in class Doctrine\Common\Cache\FileCache.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
83
84 11
        return $this;
85
    }
86
87
    /**
88
     * Sets an original restoring directory.
89
     *
90
     * @param string $directory
91
     *
92
     * @return RestorablePhpFileCache
93
     */
94 10
    protected function setRestoringDirectory($directory)
95
    {
96 10
        $this->restoringDirectory = $directory;
97
98 10
        return $this;
99
    }
100
101
    /**
102
     * Prepares temporary directory to re-create cache.
103
     *
104
     * @return RestorablePhpFileCache
105
     */
106 8
    protected function prepareTemporaryDirectory()
107
    {
108 8
        $currentDirectory = $this->getDirectory();
109 8
        $this->setRestoringDirectory($currentDirectory);
110
111
        // mkdir before setDirectory()
112 8
        $temporaryDirectory = $this->builder->build($currentDirectory);
113
114 8
        return $this->setDirectory($temporaryDirectory);
115
    }
116
117
    /**
118
     * Restores an original directory.
119
     *
120
     * @return RestorablePhpFileCache
121
     */
122 8
    protected function restoreDirectory()
123
    {
124 8
        return $this->setDirectory($this->restoringDirectory);
125
    }
126
}
127