Completed
Push — 1.0 ( 85ea96...11ed4b )
by Bernhard
04:23
created

ResourceBinding::postUnserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the puli/repository 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\Repository\Discovery;
13
14
use Puli\Discovery\Api\Binding\Binding;
15
use Puli\Discovery\Api\Binding\Initializer\NotInitializedException;
16
use Puli\Discovery\Api\Type\MissingParameterException;
17
use Puli\Discovery\Api\Type\NoSuchParameterException;
18
use Puli\Discovery\Binding\AbstractBinding;
19
use Puli\Repository\Api\ResourceCollection;
20
use Puli\Repository\Api\ResourceRepository;
21
22
/**
23
 * Binds one or more resources to a binding type.
24
 *
25
 * @since  1.0
26
 *
27
 * @author Bernhard Schussek <[email protected]>
28
 */
29
class ResourceBinding extends AbstractBinding
30
{
31
    /**
32
     * @var string
33
     */
34
    private $query;
35
36
    /**
37
     * @var string
38
     */
39
    private $language;
40
41
    /**
42
     * @var ResourceRepository
43
     */
44
    private $repo;
45
46
    /**
47
     * Creates a new resource binding.
48
     *
49
     * A resource binding has a query that is used to retrieve the resources
50
     * matched by the binding.
51
     *
52
     * You can pass parameters that have been defined for the type. If you pass
53
     * unknown parameters, or if a required parameter is missing, an exception
54
     * is thrown.
55
     *
56
     * All parameters that you do not set here will receive the default values
57
     * set for the parameter.
58
     *
59
     * @param string $query           The resource query.
60
     * @param string $typeName        The type to bind against.
61
     * @param array  $parameterValues The values of the parameters defined
62
     *                                for the type.
63
     * @param string $language        The language of the resource query.
64
     *
65
     * @throws NoSuchParameterException  If an invalid parameter was passed.
66
     * @throws MissingParameterException If a required parameter was not passed.
67
     */
68 19
    public function __construct($query, $typeName, array $parameterValues = array(), $language = 'glob')
69
    {
70 19
        parent::__construct($typeName, $parameterValues);
71
72 18
        $this->query = $query;
73 18
        $this->language = $language;
74 18
    }
75
76
    /**
77
     * Returns the query for the resources of the binding.
78
     *
79
     * @return string The resource query.
80
     */
81 1
    public function getQuery()
82
    {
83 1
        return $this->query;
84
    }
85
86
    /**
87
     * Returns the language of the query.
88
     *
89
     * @return string The query language.
90
     */
91 1
    public function getLanguage()
92
    {
93 1
        return $this->language;
94
    }
95
96
    /**
97
     * Returns the bound resources.
98
     *
99
     * @return ResourceCollection The bound resources.
100
     */
101 2
    public function getResources()
102
    {
103 2
        if (null === $this->repo) {
104 1
            throw new NotInitializedException('The repository of the resource binding must be set before accessing resources.');
105
        }
106
107 1
        return $this->repo->find($this->query, $this->language);
108
    }
109
110
    /**
111
     * Sets the repository used to load resources.
112
     *
113
     * @param ResourceRepository $repo The resource repository.
114
     */
115 1
    public function setRepository(ResourceRepository $repo)
116
    {
117 1
        $this->repo = $repo;
118 1
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function equals(Binding $other)
124
    {
125
        if (!parent::equals($other)) {
126
            return false;
127
        }
128
129
        /** @var ResourceBinding $other */
130
        if ($this->query !== $other->query) {
131
            return false;
132
        }
133
134
        return $this->language === $other->language;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 2
    protected function preSerialize(array &$data)
141
    {
142 2
        parent::preSerialize($data);
143
144 2
        $data[] = $this->query;
145 2
        $data[] = $this->language;
146 2
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 2
    protected function postUnserialize(array &$data)
152
    {
153 2
        $this->language = array_pop($data);
154 2
        $this->query = array_pop($data);
155
156 2
        parent::postUnserialize($data);
157 2
    }
158
}
159