Cancelled
Push — 0.5 ( d49ebd...32819b )
by Philippe
36s queued 36s
created

ApplyTemplate::findModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Templates;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\Relation;
9
use Webmozart\Assert\Assert;
10
11
class ApplyTemplate
12
{
13
    /** @var array */
14
    private $applicators;
15
16
    public function __construct(array $applicators)
17
    {
18
        Assert::allIsInstanceOf($applicators, TemplateApplicator::class);
19
20
        $this->applicators = $applicators;
21
    }
22
23
    public function handle(string $sourceClassName, string $sourceId, string $targetClassName, string $targetId): void
24
    {
25
        $sourceModel = $this->findModel($sourceClassName, $sourceId);
26
        $targetModel = $this->findModel($targetClassName, $targetId);
27
28
        /** @var TemplateApplicator $applicator */
29
        foreach ($this->applicators as $applicator) {
30
            if (!$applicator->shouldApply($sourceModel, $targetModel)) {
31
                continue;
32
            }
33
34
            $applicator->handle($sourceModel, $targetModel);
35
36
            return;
37
        }
38
39
        $availableApplicatorsString = implode(array_map(function ($applicator) { return get_class($applicator);}, $this->applicators), ',');
0 ignored issues
show
Unused Code introduced by
The call to implode() has too many arguments starting with ','. ( Ignorable by Annotation )

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

39
        $availableApplicatorsString = /** @scrutinizer ignore-call */ implode(array_map(function ($applicator) { return get_class($applicator);}, $this->applicators), ',');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
40
41
        throw new \RuntimeException("No proper template applicator found. $sourceClassName [$sourceId] cannot be applied as template. Available applicators: [$availableApplicatorsString]");
42
    }
43
44
    /**
45
     * For now we assume that each class references an Eloquent Model.
46
     *
47
     * @param string $className
48
     * @param string $sourceId
49
     */
50
    private function findModel(string $className, string $sourceId): Model
51
    {
52
        if ($morphedClassName = Relation::getMorphedModel($className)) {
53
            $className = $morphedClassName;
54
        }
55
56
        return (new $className())->findOrFail($sourceId);
57
    }
58
}
59