puli /
manager
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the puli/manager package. |
||
| 5 | * |
||
| 6 | * (c) Bernhard Schussek <[email protected]> |
||
| 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 Puli\Manager\Cache; |
||
| 13 | |||
| 14 | use Puli\Manager\Api\Cache\CacheFile; |
||
| 15 | use Puli\Manager\Api\FileNotFoundException; |
||
| 16 | use Puli\Manager\Api\InvalidConfigException; |
||
| 17 | use Puli\Manager\Api\Storage\ReadException; |
||
| 18 | use Puli\Manager\Api\Storage\Storage; |
||
| 19 | use Puli\Manager\Api\Storage\WriteException; |
||
| 20 | use stdClass; |
||
| 21 | use Webmozart\Json\Conversion\ConversionFailedException; |
||
| 22 | use Webmozart\Json\Conversion\JsonConverter; |
||
| 23 | use Webmozart\Json\DecodingFailedException; |
||
| 24 | use Webmozart\Json\EncodingFailedException; |
||
| 25 | use Webmozart\Json\JsonDecoder; |
||
| 26 | use Webmozart\Json\JsonEncoder; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Loads and saves cache files. |
||
| 30 | * |
||
| 31 | * @since 1.0 |
||
| 32 | * |
||
| 33 | * @author Mateusz Sojda <[email protected]> |
||
| 34 | */ |
||
| 35 | class CacheFileStorage |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var Storage |
||
| 39 | */ |
||
| 40 | private $storage; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var JsonConverter |
||
| 44 | */ |
||
| 45 | private $converter; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Creates a new storage. |
||
| 49 | * |
||
| 50 | * @param Storage $storage The file storage. |
||
| 51 | * @param JsonConverter $converter The cache file converter. |
||
| 52 | * @param JsonEncoder $jsonEncoder The JSON encoder. |
||
| 53 | * @param JsonDecoder $jsonDecoder The JSON decoder. |
||
| 54 | */ |
||
| 55 | 1 | public function __construct( |
|
| 56 | Storage $storage, |
||
| 57 | JsonConverter $converter, |
||
| 58 | JsonEncoder $jsonEncoder, |
||
| 59 | JsonDecoder $jsonDecoder |
||
| 60 | ) |
||
| 61 | { |
||
| 62 | 1 | $this->storage = $storage; |
|
| 63 | 1 | $this->converter = $converter; |
|
| 64 | 1 | $this->jsonEncoder = $jsonEncoder; |
|
|
0 ignored issues
–
show
|
|||
| 65 | 1 | $this->jsonDecoder = $jsonDecoder; |
|
|
0 ignored issues
–
show
The property
jsonDecoder does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
| 66 | 1 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Loads a cache file from a file path. |
||
| 70 | * |
||
| 71 | * @param string $path The path to the cache file. |
||
| 72 | * |
||
| 73 | * @return CacheFile The loaded cache file. |
||
| 74 | * |
||
| 75 | * @throws FileNotFoundException If the file does not exist. |
||
| 76 | * @throws ReadException If the file cannot be read. |
||
| 77 | * @throws InvalidConfigException If the file contains invalid |
||
| 78 | * configuration. |
||
| 79 | */ |
||
| 80 | public function loadCacheFile($path) |
||
| 81 | { |
||
| 82 | $serialized = $this->storage->read($path); |
||
| 83 | $jsonData = $this->decode($serialized, $path); |
||
| 84 | |||
| 85 | try { |
||
| 86 | return $this->converter->fromJson($jsonData, array( |
||
| 87 | 'path' => $path |
||
| 88 | )); |
||
| 89 | } catch (ConversionFailedException $e) { |
||
| 90 | throw new InvalidConfigException(sprintf( |
||
| 91 | 'The JSON in %s could not be converted: %s', |
||
| 92 | $path, |
||
| 93 | $e->getMessage() |
||
| 94 | ), 0, $e); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Saves a cache file. |
||
| 100 | * |
||
| 101 | * The cache file is saved to the same path that it was read from. |
||
| 102 | * |
||
| 103 | * @param CacheFile $cacheFile The cache file to save. |
||
| 104 | * |
||
| 105 | * @throws WriteException If the file cannot be written. |
||
| 106 | */ |
||
| 107 | public function saveCacheFile(CacheFile $cacheFile) |
||
| 108 | { |
||
| 109 | $jsonData = $this->converter->toJson($cacheFile); |
||
| 110 | $serialized = $this->encode($jsonData, $cacheFile->getPath()); |
||
| 111 | |||
| 112 | $this->storage->write($cacheFile->getPath(), $serialized); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns whether a cache file exists. |
||
| 117 | * |
||
| 118 | * @param string $path The cache file path. |
||
| 119 | * |
||
| 120 | * @return bool Returns `true` if the cache file exists and `false` otherwise. |
||
| 121 | */ |
||
| 122 | public function cacheFileExists($path) |
||
| 123 | { |
||
| 124 | return $this->storage->exists($path); |
||
| 125 | } |
||
| 126 | |||
| 127 | View Code Duplication | private function encode(stdClass $jsonData, $path) |
|
|
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a Loading history...
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...
|
|||
| 128 | { |
||
| 129 | try { |
||
| 130 | return $this->jsonEncoder->encode($jsonData); |
||
| 131 | } catch (EncodingFailedException $e) { |
||
| 132 | throw new InvalidConfigException(sprintf( |
||
| 133 | 'The configuration in %s could not be encoded: %s', |
||
| 134 | $path, |
||
| 135 | $e->getMessage() |
||
| 136 | ), 0, $e); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | View Code Duplication | private function decode($json, $path) |
|
|
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a Loading history...
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...
|
|||
| 141 | { |
||
| 142 | try { |
||
| 143 | return $this->jsonDecoder->decode($json); |
||
| 144 | } catch (DecodingFailedException $e) { |
||
| 145 | throw new InvalidConfigException(sprintf( |
||
| 146 | 'The configuration in %s could not be decoded: %s', |
||
| 147 | $path, |
||
| 148 | $e->getMessage() |
||
| 149 | ), 0, $e); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: