SettingFormRepository::save()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 16
c 4
b 0
f 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php namespace Anomaly\SettingsModule\Setting\Form;
2
3
use Anomaly\SettingsModule\Setting\Contract\SettingRepositoryInterface;
4
use Anomaly\SettingsModule\Setting\Event\SettingsWereSaved;
5
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
6
use Anomaly\Streams\Platform\Ui\Form\Contract\FormRepositoryInterface;
7
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
8
use Illuminate\Config\Repository;
9
use Illuminate\Container\Container;
10
use Illuminate\Events\Dispatcher;
11
12
class SettingFormRepository implements FormRepositoryInterface
13
{
14
15
    /**
16
     * The config repository.
17
     *
18
     * @var Repository
19
     */
20
    protected $config;
21
22
    /**
23
     * The event dispatcher.
24
     *
25
     * @var Dispatcher
26
     */
27
    protected $events;
28
29
    /**
30
     * The application container.
31
     *
32
     * @var Container
33
     */
34
    protected $container;
35
36
    /**
37
     * The settings repository.
38
     *
39
     * @var SettingRepositoryInterface
40
     */
41
    protected $settings;
42
43
    /**
44
     * Create a new SettingFormRepositoryInterface instance.
45
     *
46
     * @param Repository                 $config
47
     * @param Dispatcher                 $events
48
     * @param Container                  $container
49
     * @param SettingRepositoryInterface $settings
50
     */
51
    public function __construct(
52
        Repository $config,
53
        Dispatcher $events,
54
        Container $container,
55
        SettingRepositoryInterface $settings
56
    ) {
57
        $this->config    = $config;
58
        $this->events    = $events;
59
        $this->settings  = $settings;
60
        $this->container = $container;
61
    }
62
63
    /**
64
     * Find an entry or return a new one.
65
     *
66
     * @param $id
67
     * @return string
68
     */
69
    public function findOrNew($id)
70
    {
71
        return $id;
72
    }
73
74
    /**
75
     * Save the form.
76
     *
77
     * @param  FormBuilder|SettingFormBuilder $builder
78
     * @return bool|mixed
79
     */
80
    public function save(FormBuilder $builder)
81
    {
82
        $form = $builder->getForm();
83
84
        $namespace = $form->getEntry() . '::';
85
86
        /* @var FieldType $field */
87
        foreach ($form->getFields() as $field) {
88
            $key   = $namespace . $field->getField();
89
            $value = $form->getValue($field->getInputName());
90
91
            $this->settings->set($key, $value);
92
        }
93
94
        $this->events->fire(new SettingsWereSaved($builder));
95
    }
96
}
97