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 ( a48bd8...83d092 )
by joseph
16:18 queued 18s
created

EntityEmbeddableSetter::setProjectRootNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Embeddable;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\DataTransferObjects\DtoCreator;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\AbstractTestFakerDataProviderUpdater;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
9
use EdmondsCommerce\DoctrineStaticMeta\Config;
10
use gossi\codegen\model\PhpClass;
11
use gossi\codegen\model\PhpInterface;
12
use gossi\codegen\model\PhpTrait;
13
14
/**
15
 * Class EntityEmbeddableSetter
16
 *
17
 * @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Embeddable
18
 * @SuppressWarnings(PHPMD.StaticAccess)
19
 */
20
class EntityEmbeddableSetter
21
{
22
    /**
23
     * @var CodeHelper
24
     */
25
    protected $codeHelper;
26
    /**
27
     * @var NamespaceHelper
28
     */
29
    protected $namespaceHelper;
30
    /**
31
     * @var string
32
     */
33
    protected $projectRootNamespace = '';
34
    /**
35
     * @var AbstractTestFakerDataProviderUpdater
36
     */
37
    private $abstractTestFakerDataProviderUpdater;
38
    /**
39
     * @var string
40
     */
41
    private $pathToProjectRoot;
42
    /**
43
     * @var DtoCreator
44
     */
45
    private $dtoCreator;
46
47
    public function __construct(
48
        CodeHelper $codeHelper,
49
        NamespaceHelper $namespaceHelper,
50
        AbstractTestFakerDataProviderUpdater $abstractTestFakerDataProviderUpdater,
51
        Config $config,
52
        DtoCreator $dtoCreator
53
    ) {
54
        $this->codeHelper                           = $codeHelper;
55
        $this->namespaceHelper                      = $namespaceHelper;
56
        $this->abstractTestFakerDataProviderUpdater = $abstractTestFakerDataProviderUpdater;
57
        $this->pathToProjectRoot                    = $config::getProjectRootDirectory();
58
        $this->setProjectRootNamespace($this->namespaceHelper->getProjectRootNamespaceFromComposerJson());
59
        $this->dtoCreator = $dtoCreator;
60
    }
61
62
    /**
63
     * @param string $projectRootNamespace
64
     *
65
     * @return $this
66
     */
67
    public function setProjectRootNamespace(string $projectRootNamespace): self
68
    {
69
        $this->projectRootNamespace = rtrim($projectRootNamespace, '\\');
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param string $pathToProjectRoot
76
     *
77
     * @return $this
78
     * @throws \RuntimeException
79
     */
80
    public function setPathToProjectRoot(string $pathToProjectRoot): self
81
    {
82
        $realPath = \realpath($pathToProjectRoot);
83
        if (false === $realPath) {
84
            throw new \RuntimeException('Invalid path to project root ' . $pathToProjectRoot);
85
        }
86
        $this->pathToProjectRoot = $realPath;
87
88
        return $this;
89
    }
90
91
    public function setEntityHasEmbeddable(string $entityFqn, string $embeddableTraitFqn): void
92
    {
93
        $entityReflection          = new \ts\Reflection\ReflectionClass($entityFqn);
94
        $entity                    = PhpClass::fromFile($entityReflection->getFileName());
95
        $entityInterfaceFqn        = $this->namespaceHelper->getEntityInterfaceFromEntityFqn($entityFqn);
96
        $entityInterfaceReflection = new \ts\Reflection\ReflectionClass($entityInterfaceFqn);
97
        $entityInterface           = PhpInterface::fromFile($entityInterfaceReflection->getFileName());
98
        $embeddableReflection      = new \ts\Reflection\ReflectionClass($embeddableTraitFqn);
99
        $trait                     = PhpTrait::fromFile($embeddableReflection->getFileName());
100
        $interfaceFqn              = \str_replace(
101
            '\Traits\\',
102
            '\Interfaces\\',
103
            $embeddableTraitFqn
104
        );
105
        $interfaceFqn              = $this->namespaceHelper->swapSuffix($interfaceFqn, 'Trait', 'Interface');
106
        $interfaceReflection       = new \ts\Reflection\ReflectionClass($interfaceFqn);
107
        $interface                 = PhpInterface::fromFile($interfaceReflection->getFileName());
108
        $entity->addTrait($trait);
109
        $this->codeHelper->generate($entity, $entityReflection->getFileName());
110
        $entityInterface->addInterface($interface);
111
        $this->codeHelper->generate($entityInterface, $entityInterfaceReflection->getFileName());
112
        $this->abstractTestFakerDataProviderUpdater->updateFakerProviderArrayWithEmbeddableFakerData(
113
            $this->pathToProjectRoot,
114
            $embeddableTraitFqn,
115
            $entityFqn
116
        );
117
        $this->dtoCreator->setNewObjectFqnFromEntityFqn($entityFqn)
118
                         ->setProjectRootDirectory($this->pathToProjectRoot)
119
                         ->setProjectRootNamespace($this->projectRootNamespace)
120
                         ->createTargetFileObject()
121
                         ->write();
122
    }
123
}
124