Test Setup Failed
Push — ft/duplicate-page ( 3cb9e2 )
by Ben
65:03 queued 56:56
created

PageTemplateApplicator::__construct()   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 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Thinktomorrow\Chief\Templates;
5
6
use Thinktomorrow\Chief\Modules\Module;
7
use Thinktomorrow\Chief\Pages\Page;
8
use Thinktomorrow\Chief\Relations\ActsAsParent;
9
use Webmozart\Assert\Assert;
10
11
class PageTemplateApplicator implements TemplateApplicator
12
{
13
    /** @var ModuleTemplateApplicator */
14
    private $moduleTemplateApplicator;
15
16
    public function __construct(ModuleTemplateApplicator $moduleTemplateApplicator)
17
    {
18
        $this->moduleTemplateApplicator = $moduleTemplateApplicator;
19
    }
20
21
    public function handle($sourceModel, $targetModel): void
22
    {
23
        /** @var Page $sourceModel */
24
        Assert::isInstanceOf($sourceModel, Page::class);
25
26
        if ($this->shouldApplyRelations($sourceModel, $targetModel))
27
        {
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
        {
53
            $duplicatedChild = null;
54
55
            if ($child instanceof Module && $child->isPageSpecific())
56
            {
57
                $duplicatedChild = $child::create([
58
                    '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...
59
                    'page_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...
60
                    'page_morph_key' => $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

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