ModelCollectionFactoryPrinter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Leonidas\Console\Library\Printer\Model;
4
5
use Leonidas\Console\Library\Printer\Model\Abstracts\AbstractClassPrinter;
6
use Leonidas\Contracts\System\Schema\EntityCollectionFactoryInterface;
7
use Nette\PhpGenerator\PhpNamespace;
8
9
class ModelCollectionFactoryPrinter extends AbstractClassPrinter
10
{
11
    protected string $collection;
12
13
    public function __construct(string $namespace, string $class, string $collection)
14
    {
15
        parent::__construct($namespace, $class);
16
        $this->collection = $collection;
17
    }
18
19
    protected function setupClass(PhpNamespace $namespace): object
20
    {
21
        $base = EntityCollectionFactoryInterface::class;
22
        $return = explode('\\', $this->collection);
23
        $return = end($return);
24
25
        $class = $namespace->addUse($base)->addClass($this->class);
26
27
        $class->addImplement($base);
28
        $class->addMethod('createEntityCollection')
29
            ->setVariadic(true)
30
            ->setReturnType($this->collection)
31
            ->setBody(sprintf('return new %s(...$entities);', $return))
32
            ->addParameter('entities')
33
            ->setType('object');
34
35
        return $class;
36
    }
37
}
38