Completed
Push — 24-pages-form-conversion ( d5ad41...7a3bca )
by jelmer
61:38 queued 12s
created

PageCopyToOtherLanguage::setKernel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Backend\Modules\Pages\Actions;
4
5
use Backend\Core\Engine\Base\ActionIndex as BackendBaseActionIndex;
6
use Backend\Core\Engine\Exception as BackendException;
7
use Backend\Core\Engine\Model as BackendModel;
8
use Backend\Core\Language\Locale;
9
use Backend\Modules\Pages\Domain\Page\CopyPageDataTransferObject;
10
use Backend\Modules\Pages\Domain\Page\Form\CopyPageToOtherLanguageType;
11
use Backend\Modules\Pages\Domain\Page\PageRepository;
12
use Exception;
13
use ForkCMS\Utility\Module\CopyContentToOtherLocale\CopyContentFromModulesToOtherLocaleManager;
14
use Symfony\Component\HttpKernel\KernelInterface;
15
16
/**
17
 * BackendPagesCopy
18
 * This is the copy-action, it will copy pages from one language to another
19
 * Remark :    IMPORTANT existing data will be removed, this feature is also experimental!
20
 */
21
class PageCopyToOtherLanguage extends BackendBaseActionIndex
22
{
23
    /** @var CopyContentFromModulesToOtherLocaleManager */
24
    private $copyManager;
25
26
    public function setKernel(KernelInterface $kernel = null): void
27
    {
28
        parent::setKernel($kernel);
29
30
        $this->copyManager = $this->getContainer()->get(CopyContentFromModulesToOtherLocaleManager::class);
31
    }
32
33
    public function execute(): void
34
    {
35
        // call parent, this will probably add some general CSS/JS or other required files
36
        parent::execute();
37
38
        $form = $this->createForm(CopyPageToOtherLanguageType::class, new CopyPageDataTransferObject());
39
        $form->handleRequest($this->getRequest());
40
41
        if (!$form->isSubmitted() || !$form->isValid()) {
42
            $this->redirect(BackendModel::createUrlForAction('PageIndex') . '&error=error-copy');
43
        }
44
45
        /** @var CopyPageDataTransferObject $data */
46
        $data = $form->getData();
47
48
        // validate
49
        if ($data->from === null) {
50
            throw new BackendException('Specify a from parameter.');
51
        }
52
        if ($data->to === null) {
53
            throw new BackendException('Specify a to parameter.');
54
        }
55
56
        // copy pages
57
        try {
58
            $this->copyManager->copyOne(
59
                $data->pageToCopy,
0 ignored issues
show
Bug introduced by
It seems like $data->pageToCopy can also be of type null; however, parameter $pageToCopy of ForkCMS\Utility\Module\C...ocaleManager::copyOne() does only seem to accept Backend\Modules\Pages\Domain\Page\Page, maybe add an additional type check? ( Ignorable by Annotation )

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

59
                /** @scrutinizer ignore-type */ $data->pageToCopy,
Loading history...
60
                Locale::fromString($data->from),
61
                Locale::fromString($data->to)
62
            );
63
        } catch (Exception $e) {
64
            $this->redirect(
65
                BackendModel::createUrlForAction('PageEdit')
66
                . '&id='
67
                . $data->pageToCopy->getId()
0 ignored issues
show
Bug introduced by
The method getId() does not exist on null. ( Ignorable by Annotation )

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

67
                . $data->pageToCopy->/** @scrutinizer ignore-call */ getId()

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...
68
                . '&error=error-copy'
69
            );
70
        }
71
72
        // redirect
73
        $this->redirect(
74
            BackendModel::createUrlForAction('PageIndex') . '&report=copy-added&var=' . rawurlencode($data->to)
75
        );
76
    }
77
}
78