RawDefinition::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2021 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI;
19
20
use PhpParser\BuilderFactory;
21
use Rade\DI\Exceptions\ContainerResolutionException;
22
23
/**
24
 * Represents a service which should not be resolved.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
class RawDefinition
29
{
30
    /** @var mixed */
31
    private $service;
32
33
    /**
34
     * @param mixed $service
35
     */
36 23
    public function __construct($service)
37
    {
38 23
        if ($service instanceof self) {
39 1
            throw new ContainerResolutionException('unresolvable definition cannot contain itself.');
40
        }
41
42 23
        $this->service = $service;
43 23
    }
44
45
    /**
46
     * @return mixed $service
47
     */
48 13
    public function __invoke()
49
    {
50 13
        return $this->service;
51
    }
52
53
    /**
54
     * Build the raw definition service.
55
     */
56 3
    public function build(string $id, BuilderFactory $builder): \PhpParser\Builder\Method
57
    {
58 3
        return $builder->method(Definition::createMethod($id))->makeProtected()
59 3
            ->addStmt(new \PhpParser\Node\Stmt\Return_($builder->val($this->service)));
60
    }
61
}
62