Completed
Push — develop ( 50e66f...5c7133 )
by Siad
04:45
created

EnvContextFactory::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 6
1
<?php
2
/**
3
 * zf2-featureflags.
4
 *
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
11
 * SOFTWARE.
12
 *
13
 * @copyright 2016 MehrAlsNix (http://www.mehralsnix.de)
14
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
15
 *
16
 * @link      http://github.com/MehrAlsNix/zf2-featureflags
17
 */
18
19
namespace MehrAlsNix\FeatureToggle\Factory;
20
21
use Interop\Container\ContainerInterface;
22
use MehrAlsNix\FeatureToggle\Context\EnvContext;
23
use Qandidate\Toggle\Context;
24
use Zend\Http\PhpEnvironment\Request;
25
use Zend\ServiceManager\Exception\ServiceNotFoundException;
26
use Zend\ServiceManager\Factory\FactoryInterface;
27
use Zend\ServiceManager\ServiceLocatorInterface;
28
29
class EnvContextFactory implements FactoryInterface
30
{
31
    private $name = '';
32
33
    public function __construct($name = '')
34
    {
35
        $this->name = $name;
36
    }
37
38
    public function __invoke(ContainerInterface $container, $requestedName = '', array $options = null)
39
    {
40
        $name = $this->name;
41
42
        if ($name === '') {
43
            $name = $container->get('Config')['zf2_featureflags']['envContext'];
44
        }
45
46
        /** @var Request $request */
47
        $request = $container->get('Request');
48
49
        $envContext = new EnvContext($name, $request);
50
51
        return $envContext->createContext();
52
    }
53
54
    /**
55
     * Create service
56
     *
57
     * @param ServiceLocatorInterface $serviceLocator
58
     *
59
     * @return Context
60
     *
61
     * @throws ServiceNotFoundException
62
     */
63
    public function createService(ServiceLocatorInterface $serviceLocator)
64
    {
65
        return $this($serviceLocator->getServiceLocator());
66
    }
67
}
68