GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f81508...2db5b7 )
by Théo
55:20 queued 20:22
created

InstantiatedReferenceInstantiator::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Hautelook\AliceBundle package.
5
 *
6
 * (c) Baldur Rensch <[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
declare(strict_types = 1);
13
14
namespace Hautelook\AliceBundle\Alice\Generator\Instantiator\Chainable;
15
16
use Nelmio\Alice\Definition\MethodCall\MethodCallWithReference;
17
use Nelmio\Alice\Definition\Object\SimpleObject;
18
use Nelmio\Alice\Definition\ServiceReference\InstantiatedReference;
19
use Nelmio\Alice\Exception\Generator\Instantiator\InstantiationException;
20
use Nelmio\Alice\FixtureInterface;
21
use Nelmio\Alice\Generator\GenerationContext;
22
use Nelmio\Alice\Generator\Instantiator\ChainableInstantiatorInterface;
23
use Nelmio\Alice\Generator\ResolvedFixtureSet;
24
use Nelmio\Alice\NotClonableTrait;
25
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
26
use Symfony\Component\DependencyInjection\ContainerInterface;
27
28
/**
29
 * @author Théo FIDRY <[email protected]>
30
 */
31
final class InstantiatedReferenceInstantiator implements ChainableInstantiatorInterface, ContainerAwareInterface
32
{
33
    use NotClonableTrait;
34
35
    /**
36
     * @var ContainerInterface|null
37
     */
38
    private $container;
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function setContainer(ContainerInterface $container = null)
44
    {
45
        $this->container = $container;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function canInstantiate(FixtureInterface $fixture): bool
52
    {
53
        $constructor = $fixture->getSpecs()->getConstructor();
54
55
        return (
56
            null !== $constructor
57
            && $constructor instanceof MethodCallWithReference
58
            && $constructor->getCaller() instanceof InstantiatedReference
59
        );
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function instantiate(
66
        FixtureInterface $fixture,
67
        ResolvedFixtureSet $fixtureSet,
68
        GenerationContext $context
69
    ): ResolvedFixtureSet
70
    {
71
        $this->checkContainer(__METHOD__);
72
        $instance = $this->createInstance($fixture);
73
74
        return $this->generateSet($fixture, $fixtureSet, $instance);
75
    }
76
77
    private function checkContainer(string $method)
78
    {
79
        if (null === $this->container) {
80
            throw new \LogicException(
81
                sprintf(
82
                    'Expected instantiator method "%s" to be used only if it has a container, but no container could'
83
                    .' be found.',
84
                    $method
85
                )
86
            );
87
        }
88
    }
89
90
    private function createInstance(FixtureInterface $fixture)
91
    {
92
        $constructor = $fixture->getSpecs()->getConstructor();
93
        list($class, $factoryReference, $method, $arguments) = [
94
            $fixture->getClassName(),
95
            $constructor->getCaller()->getId(),
96
            $constructor->getMethod(),
97
            $constructor->getArguments()
98
        ];
99
100
        if (null === $arguments) {
101
            $arguments = [];
102
        }
103
104
        $factory = $this->container->get($factoryReference);
105
106
        $instance = $factory->$method(...$arguments);
107
        if (false === $instance instanceof $class) {
108
            throw new InstantiationException(
109
                sprintf(
110
                    'Instantiated fixture was expected to be an instance of "%s". Got "%s" instead.',
111
                    $class,
112
                    get_class($instance)
113
                )
114
            );
115
        }
116
117
        return $instance;
118
    }
119
120
    private function generateSet(
121
        FixtureInterface $fixture,
122
        ResolvedFixtureSet $fixtureSet,
123
        $instance
124
    ): ResolvedFixtureSet
125
    {
126
        $objects = $fixtureSet->getObjects()->with(
127
            new SimpleObject(
128
                $fixture->getId(),
129
                $instance
130
            )
131
        );
132
133
        return $fixtureSet->withObjects($objects);
134
    }
135
}
136