Completed
Pull Request — master (#7)
by Thomas Mauro
05:04
created

AbstractFactory::getOptionsClass()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\SentryModule\Service;
6
7
use Interop\Container\ContainerInterface;
8
use Facile\SentryModule\Exception\RuntimeException;
9
use Zend\ServiceManager\FactoryInterface;
10
11
/**
12
 * Class AbstractFactory.
13
 */
14
abstract class AbstractFactory implements FactoryInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Zend\ServiceManager\FactoryInterface has been deprecated with message: Use Zend\ServiceManager\Factory\FactoryInterface instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
15
{
16
    /**
17
     * @var null|string
18
     */
19
    protected $name;
20
21
    /**
22
     * @var \Zend\Stdlib\AbstractOptions
23
     */
24
    protected $options;
25
26
    /**
27
     * @var string
28
     */
29
    protected $configKey = 'facile';
30
31
    /**
32
     * @param string $name
33
     */
34 2
    public function __construct($name)
35
    {
36 2
        $this->name = $name;
37 2
    }
38
39
    /**
40
     * @return null|string
41
     */
42 2
    public function getName()
43
    {
44 2
        return $this->name;
45
    }
46
47
    /**
48
     * Gets options from configuration based on name.
49
     *
50
     * @param ContainerInterface $container
51
     * @param string $key
52
     * @param null|string $name
53
     *
54
     * @return \Zend\Stdlib\AbstractOptions
55
     * @throws \Facile\SentryModule\Exception\RuntimeException
56
     *
57
     * @throws \Interop\Container\Exception\NotFoundException
58
     * @throws \Interop\Container\Exception\ContainerException
59
     */
60 2
    public function getOptions(ContainerInterface $container, $key, $name = null)
61
    {
62 2
        if ($name === null) {
63 2
            $name = $this->getName();
64
        }
65
66
        /** @var array $options */
67 2
        $options = $container->get('config');
68 2
        $options = $options[$this->configKey]['sentry'];
69 2
        $options = isset($options[$key][$name]) ? $options[$key][$name] : null;
70
71 2
        if (null === $options) {
72
            throw new RuntimeException(
73
                sprintf('Options with name "%s" could not be found in "%s.%s"', $name, $this->configKey, $key)
74
            );
75
        }
76
77 2
        $optionsClass = $this->getOptionsClass();
78
79 2
        return new $optionsClass($options);
80
    }
81
82
    /**
83
     * Get the class name of the options associated with this factory.
84
     *
85
     * @abstract
86
     *
87
     * @return string
88
     */
89
    abstract public function getOptionsClass();
90
}
91