Passed
Push — dev_2x ( 241dab...1e248d )
by Adrian
01:59
created

ManyToManyObserver::observe()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\CodeGenerator\Observer\Relation;
5
6
use Nette\PhpGenerator\ClassType;
7
use Sirius\Orm\Blueprint\Mapper;
8
use Sirius\Orm\Contract\Relation\ToManyInterface;
9
use Sirius\Orm\Helpers\Str;
10
11
class ManyToManyObserver extends OneToManyObserver implements ToManyInterface
12
{
13
    public function observe(string $key, $object)
14
    {
15
        if ($key === $this->relation->getForeignMapper() . '_base_entity') {
16
            return $this->observeLinkedBaseEntity($object);
17
        }
18
19
        return parent::observe($key, $object);
20
    }
21
22
    protected function observeLinkedBaseEntity(ClassType $class)
23
    {
24
        $throughColumns = $this->relation->getThroughColumns();
0 ignored issues
show
Bug introduced by
The method getThroughColumns() does not exist on Sirius\Orm\Blueprint\Relation\OneToMany. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        /** @scrutinizer ignore-call */ 
25
        $throughColumns = $this->relation->getThroughColumns();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
        if (empty($throughColumns)) {
26
            return $class;
27
        }
28
29
        foreach ($throughColumns as $column) {
30
            $comment = 'unsed only for relations with ' . $this->relation->getMapper()->getName();
31
            if ($this->relation->getMapper()->getEntityStyle() === Mapper::ENTITY_STYLE_PROPERTIES) {
32
                $class->addComment(sprintf('@property mixed $%s - %s', $column, $comment));
33
            } else {
34
                $setter = $class->addMethod(Str::methodName($column, 'set'));
35
                $setter->setVisibility(ClassType::VISIBILITY_PUBLIC);
36
                $setter->addParameter('value');
37
                $setter->addBody('$this->set(\'' . $column . '\', $value);');
38
                $setter->setComment($comment);
39
40
                $getter = $class->addMethod(Str::methodName($column, 'get'));
41
                $getter->setVisibility(ClassType::VISIBILITY_PUBLIC);
42
                $getter->addBody('return $this->get(\'' . $column . '\');');
43
                $setter->setComment($comment);
44
            }
45
        }
46
47
        return $class;
48
    }
49
}
50