RequireWriteableContainer::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
/**
4
 * Copyright (c) 2016-present Ganbaro Digital Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   DIContainers/V1/Requirements
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2016-present Ganbaro Digital Ltd www.ganbarodigital.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://ganbarodigital.github.io/php-mv-di-containers
42
 */
43
44
namespace GanbaroDigital\DIContainers\V1\Requirements;
45
46
use GanbaroDigital\DIContainers\V1\Exceptions\ContainerIsReadOnly;
47
use GanbaroDigital\DIContainers\V1\Exceptions\DIContainersExceptions;
48
use GanbaroDigital\DIContainers\V1\Interfaces\FactoryList;
49
50
/**
51
 * make sure that we are working with a writeable container
52
 */
53
class RequireWriteableContainer
54
{
55
    /**
56
     * a list of exception functions
57
     * @var FactoryList
58
     */
59
    private $exceptions;
60
61
    /**
62
     * a call stack filter to apply
63
     * @var array
64
     */
65
    private $defaultCallStackFilter = [];
0 ignored issues
show
Unused Code introduced by
The property $defaultCallStackFilter is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
66
67
    /**
68
     * create a requirement that is ready to execute
69
     *
70
     * @param  FactoryList|null $exceptions
71
     *         the functions to call when we want to throw an exception
72
     * @param  array $callStackFilter
73
     *         a list of classes to filter from the call stack analysis
74
     * @return RequireWriteableContainer
75
     */
76
    static public function apply(FactoryList $exceptions = null, array $callStackFilter = [])
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
77
    {
78
        return new static($exceptions, $callStackFilter);
79
    }
80
81
    /**
82
     * our constructor
83
     *
84
     * @param  FactoryList|null $exceptions
85
     *         the functions to call when we want to throw an exception
86
     * @param  array $callStackFilter
87
     *         a list of classes to filter from the call stack analysis
88
     */
89
    protected function __construct(FactoryList $exceptions = null, array $callStackFilter = [])
90
    {
91
        if ($exceptions === null) {
92
            $exceptions = new DIContainersExceptions;
93
        }
94
        $this->exceptions = $exceptions;
95
        $this->callStackFilter = $callStackFilter;
0 ignored issues
show
Bug introduced by
The property callStackFilter does not seem to exist. Did you mean defaultCallStackFilter?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
96
    }
97
98
    /**
99
     * throws exception if our requirement is not met
100
     *
101
     * @param  FactoryList $container
102
     *         the container to examine
103
     * @param  string $fieldOrVarName
104
     *         what is the name of $container in the calling code?
105
     * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
106
     */
107
    public function to(FactoryList $container, $fieldOrVarName = '$container')
108
    {
109
        if ($container->isReadWrite()) {
110
            return true;
111
        }
112
113
        throw $this->exceptions["ContainerIsReadOnly::newFromInputParameter"]($container, $fieldOrVarName, [], null, $this->callStackFilter);
0 ignored issues
show
Bug introduced by
The property callStackFilter does not seem to exist. Did you mean defaultCallStackFilter?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
114
    }
115
}
116