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

PageTemplateApplicator::shouldApply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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
        // which parts to provide?
33
        //    public function children(): Collection;
34
        //    public function values(): array;
35
        //    public function translations(): array;
36
        //    public function fragments(): array;
37
    }
38
39
    public function shouldApply($sourceModel, $targetModel): bool
40
    {
41
        return ($sourceModel instanceof Page);
42
    }
43
44
    private function shouldApplyRelations($sourceModel, $targetModel): bool
45
    {
46
        return ($sourceModel instanceof ActsAsParent && $targetModel instanceof ActsAsParent);
47
    }
48
49
    private function applyRelations(ActsAsParent $sourceModel, ActsAsParent $targetModel): void
50
    {
51
        foreach ($sourceModel->children() as $child) {
52
            $duplicatedChild = null;
53
54
            if ($child instanceof Module && $child->isPageSpecific()) {
55
                $duplicatedChild = $child::create([
56
                    '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...
57
                    '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...
58
                    '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

58
                    'owner_type' => $targetModel->/** @scrutinizer ignore-call */ getMorphClass()
Loading history...
59
                ]);
60
61
                $this->moduleTemplateApplicator->handle($child, $duplicatedChild);
62
            } else {
63
                $duplicatedChild = $child;
64
            }
65
66
            $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...
67
        }
68
    }
69
}
70