Completed
Push — 2.0 ( d9081a...4a1e3c )
by Rob
11:15
created

ResolveCache::jsonDeserialize()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Async;
13
14
use Enqueue\Util\JSON;
15
use Liip\ImagineBundle\Exception\LogicException;
16
17
class ResolveCache implements \JsonSerializable
18
{
19
    /**
20
     * @var string
21
     */
22
    private $path;
23
24
    /**
25
     * @var array|null|\string[]
26
     */
27
    private $filters;
28
29
    /**
30
     * @var bool
31
     */
32
    private $force;
33
34
    /**
35
     * @param string        $path
36
     * @param string[]|null $filters
37
     * @param bool          $force
38
     */
39
    public function __construct(string $path, array $filters = null, bool $force = false)
40
    {
41
        $this->path = $path;
42
        $this->filters = $filters;
43
        $this->force = $force;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getPath(): string
50
    {
51
        return $this->path;
52
    }
53
54
    /**
55
     * @return null|\string[]
56
     */
57
    public function getFilters()
58
    {
59
        return $this->filters;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isForce(): bool
66
    {
67
        return $this->force;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function jsonSerialize(): array
74
    {
75
        return ['path' => $this->path, 'filters' => $this->filters, 'force' => $this->force];
76
    }
77
78
    /**
79
     * @param string $json
80
     *
81
     * @return self
82
     */
83
    public static function jsonDeserialize(string $json): self
84
    {
85
        $data = array_replace(['path' => null, 'filters' => null, 'force' => false], JSON::decode($json));
86
87
        if (false == $data['path']) {
88
            throw new LogicException('The message does not contain "path" but it is required.');
89
        }
90
91
        if (false == (is_null($data['filters']) || is_array($data['filters']))) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
92
            throw new LogicException('The message filters could be either null or array.');
93
        }
94
95
        return new static($data['path'], $data['filters'], $data['force']);
96
    }
97
}
98