Contact   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 21
c 2
b 0
f 0
dl 0
loc 78
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A hasAnyChangedSince() 0 3 1
A load() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Contact\Template\Loader;
6
7
use AbterPhp\Contact\Constant\Event;
8
use AbterPhp\Contact\Constant\Route;
9
use AbterPhp\Contact\Form\Factory\Contact as FormFactory;
10
use AbterPhp\Framework\Events\FormReady;
11
use AbterPhp\Framework\I18n\ITranslator;
12
use AbterPhp\Framework\Template\Data;
13
use AbterPhp\Framework\Template\IData;
14
use AbterPhp\Framework\Template\ILoader;
15
use AbterPhp\Framework\Template\ParsedTemplate;
16
use Opulence\Events\Dispatchers\IEventDispatcher;
17
use Opulence\Http\Requests\RequestMethods;
18
use Opulence\Routing\Urls\UrlGenerator;
19
20
class Contact implements ILoader
21
{
22
    /** @var UrlGenerator */
23
    protected $urlGenerator;
24
25
    /** @var FormFactory */
26
    protected $formFactory;
27
28
    /** @var ITranslator */
29
    protected $translator;
30
31
    /** @var IEventDispatcher */
32
    protected $eventDispatcher;
33
34
    /**
35
     * Contact constructor.
36
     *
37
     * @param UrlGenerator     $urlGenerator
38
     * @param FormFactory      $formFactory
39
     * @param ITranslator      $translator
40
     * @param IEventDispatcher $eventDispatcher
41
     */
42
    public function __construct(
43
        UrlGenerator $urlGenerator,
44
        FormFactory $formFactory,
45
        ITranslator $translator,
46
        IEventDispatcher $eventDispatcher
47
    ) {
48
        $this->urlGenerator    = $urlGenerator;
49
        $this->formFactory     = $formFactory;
50
        $this->translator      = $translator;
51
        $this->eventDispatcher = $eventDispatcher;
52
    }
53
54
    /**
55
     * @param ParsedTemplate[] $parsedTemplates
56
     *
57
     * @return IData[]
58
     */
59
    public function load(array $parsedTemplates): array
60
    {
61
        $identifiers = array_keys($parsedTemplates);
62
63
        $templateData = [];
64
        foreach ($identifiers as $identifier) {
65
            // @phan-suppress-next-line PhanTypeMismatchArgument
66
            $url  = $this->urlGenerator->createFromName(Route::CONTACT, $identifier);
67
            $form = $this->formFactory->create($url, RequestMethods::POST, '');
68
69
            $form->setTranslator($this->translator);
70
71
            $this->eventDispatcher->dispatch(Event::FORM_READY, new FormReady($form));
72
73
            $templateData[] = new Data(
74
                $identifier,
75
                [],
76
                [(string)$form]
77
            );
78
        }
79
80
        return $templateData;
81
    }
82
83
    /**
84
     * Because the form will come with a non-reusable CSRF token, it needs to invalidate the page if it contains
85
     * a contact form
86
     * (It's possible to work around this, but it wouldn't be easy as Opulence checks CSRF tokens automatically.)
87
     *
88
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
89
     *
90
     * @param string[] $identifiers
91
     * @param string   $cacheTime
92
     *
93
     * @return bool
94
     */
95
    public function hasAnyChangedSince(array $identifiers, string $cacheTime): bool
96
    {
97
        return count($identifiers) > 0;
98
    }
99
}
100