Completed
Push — master ( d80cf4...f87515 )
by Gianluca
04:15
created

AbstractFactory::getOptionsClass()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineModule\Service;
21
22
use RuntimeException;
23
use Zend\ServiceManager\FactoryInterface;
24
use Zend\ServiceManager\ServiceLocatorInterface;
25
26
/**
27
 * Base ServiceManager factory to be extended
28
 *
29
 * @license MIT
30
 * @link    http://www.doctrine-project.org/
31
 * @author  Kyle Spraggs <[email protected]>
32
 */
33
abstract class AbstractFactory implements FactoryInterface
34
{
35
    /**
36
     * Would normally be set to orm | odm
37
     *
38
     * @var string
39
     */
40
    protected $mappingType;
41
42
    /**
43
     * @var string
44
     */
45
    protected $name;
46
47
    /**
48
     * @var \Zend\Stdlib\AbstractOptions
49
     */
50
    protected $options;
51
52
    /**
53
     * @param string $name
54
     */
55 12
    public function __construct($name)
56
    {
57 12
        $this->name = $name;
58 12
    }
59
60
    /**
61
     * @return string
62
     */
63 12
    public function getName()
64
    {
65 12
        return $this->name;
66
    }
67
68
    /**
69
     * Would normally be set to orm | odm
70
     *
71
     * @return string
72
     */
73 12
    public function getMappingType()
74
    {
75 12
        return $this->mappingType;
76
    }
77
78
    /**
79
     * Gets options from configuration based on name.
80
     *
81
     * @param  ServiceLocatorInterface      $sl
82
     * @param  string                       $key
83
     * @param  null|string                  $name
84
     * @return \Zend\Stdlib\AbstractOptions
85
     * @throws \RuntimeException
86
     */
87 12
    public function getOptions(ServiceLocatorInterface $sl, $key, $name = null)
88
    {
89 12
        if ($name === null) {
90 12
            $name = $this->getName();
91 12
        }
92
93 12
        $options = $sl->get('Configuration');
94 12
        $options = $options['doctrine'];
95 12
        if ($mappingType = $this->getMappingType()) {
96
            $options = $options[$mappingType];
97
        }
98 12
        $options = isset($options[$key][$name]) ? $options[$key][$name] : null;
99
100 12
        if (null === $options) {
101
            throw new RuntimeException(
102
                sprintf(
103
                    'Options with name "%s" could not be found in "doctrine.%s".',
104
                    $name,
105
                    $key
106
                )
107
            );
108
        }
109
110 12
        $optionsClass = $this->getOptionsClass();
111
112 12
        return new $optionsClass($options);
113
    }
114
115
    /**
116
     * Get the class name of the options associated with this factory.
117
     *
118
     * @abstract
119
     * @return string
120
     */
121
    abstract public function getOptionsClass();
122
}
123