Completed
Pull Request — master (#2)
by Beñat
02:38
created

LocalStorageAction::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPSymfonyForm\Action;
13
14
use Symfony\Component\Form\FormInterface;
15
use Symfony\Component\Yaml\Yaml;
16
17
/**
18
 * Local storage action.
19
 *
20
 * @author Beñat Espiña <[email protected]>
21
 */
22
class LocalStorageAction implements Action
23
{
24
    /**
25
     * The dir path of YAML file.
26
     *
27
     * @var string
28
     */
29
    private $yamlFile;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param string|null $yamlFile The dir path of YAML file
35
     */
36
    public function __construct($yamlFile = null)
37
    {
38
        $this->yamlFile = __DIR__ . '/../../../../../../../wp_symfony_form_email_log.yml';
39
        if (null !== $yamlFile) {
40
            $this->yamlFile = $yamlFile;
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function execute(FormInterface $form)
48
    {
49
        $existentData = Yaml::parse(file_get_contents($this->yamlFile));
50
        $newData = [
51
            'formType' => $form->getName(),
52
            'date'     => (new \DateTimeImmutable())->format('Y-m-d - h:m'),
53
        ];
54
        $newData = array_merge($newData, $form->getData());
55
56
        $data = null !== $existentData ? array_merge($existentData, [$newData]) : [$newData];
57
58
        file_put_contents($this->yamlFile, Yaml::dump($data));
59
    }
60
}
61