Passed
Push — master ( 00089d...cdf2c0 )
by Philippe
08:10 queued 12s
created

PageTemplateApplicator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 19
c 2
b 0
f 0
dl 0
loc 48
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 7 2
A shouldApply() 0 3 1
A applyRelations() 0 18 5
A shouldApplyRelations() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Templates;
6
7
use Thinktomorrow\Chief\Modules\Module;
8
use Thinktomorrow\Chief\Pages\Page;
9
use Thinktomorrow\Chief\Relations\ActsAsParent;
10
use Webmozart\Assert\Assert;
11
12
class PageTemplateApplicator implements TemplateApplicator
13
{
14
    /** @var ModuleTemplateApplicator */
15
    private $moduleTemplateApplicator;
16
17
    public function __construct(ModuleTemplateApplicator $moduleTemplateApplicator)
18
    {
19
        $this->moduleTemplateApplicator = $moduleTemplateApplicator;
20
    }
21
22
    public function handle($sourceModel, $targetModel): void
23
    {
24
        /** @var Page $sourceModel */
25
        Assert::isInstanceOf($sourceModel, Page::class);
26
27
        if ($this->shouldApplyRelations($sourceModel, $targetModel)) {
28
            $this->applyRelations($sourceModel, $targetModel);
29
        }
30
    }
31
32
    public function shouldApply($sourceModel, $targetModel): bool
33
    {
34
        return ($sourceModel instanceof Page);
35
    }
36
37
    private function shouldApplyRelations($sourceModel, $targetModel): bool
38
    {
39
        return ($sourceModel instanceof ActsAsParent && $targetModel instanceof ActsAsParent);
40
    }
41
42
    private function applyRelations(ActsAsParent $sourceModel, ActsAsParent $targetModel): void
43
    {
44
        foreach ($sourceModel->children() as $child) {
45
            $duplicatedChild = null;
46
47
            if ($child instanceof Module && $child->isPageSpecific()) {
48
                $duplicatedChild = $child::create([
49
                    'slug'           => $targetModel->title ? $targetModel->title . '-' . $child->slug : $child->slug . '-copy',
0 ignored issues
show
Bug introduced by
Accessing title on the interface Thinktomorrow\Chief\Relations\ActsAsParent suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
50
                    'owner_id'        => $targetModel->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Thinktomorrow\Chief\Relations\ActsAsParent suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
51
                    'owner_type' => $targetModel->getMorphClass()
0 ignored issues
show
Bug introduced by
The method getMorphClass() does not exist on Thinktomorrow\Chief\Relations\ActsAsParent. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\Chief\Relations\ActsAsParent. ( Ignorable by Annotation )

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

51
                    'owner_type' => $targetModel->/** @scrutinizer ignore-call */ getMorphClass()
Loading history...
52
                ]);
53
54
                $this->moduleTemplateApplicator->handle($child, $duplicatedChild);
55
            } else {
56
                $duplicatedChild = $child;
57
            }
58
59
            $targetModel->adoptChild($duplicatedChild, ['sort' => $child->relation->sort]);
0 ignored issues
show
Bug introduced by
The property relation does not exist on Thinktomorrow\Chief\Modules\Module. Did you mean relations?
Loading history...
60
        }
61
    }
62
}
63