Configuration   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 121
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 54 2
A addResourcesSection() 0 46 1
A addQueueOptions() 0 5 1
1
<?php
2
3
/** @noinspection NullPointerExceptionInspection */
4
5
declare(strict_types=1);
6
7
namespace Setono\SyliusSchedulerPlugin\DependencyInjection;
8
9
use Setono\SyliusSchedulerPlugin\Doctrine\ORM\JobRepository;
10
use Setono\SyliusSchedulerPlugin\Doctrine\ORM\ScheduleRepository;
11
use Setono\SyliusSchedulerPlugin\Form\Type\ScheduleType;
12
use Setono\SyliusSchedulerPlugin\Model\Job;
13
use Setono\SyliusSchedulerPlugin\Model\JobInterface;
14
use Setono\SyliusSchedulerPlugin\Model\Schedule;
15
use Setono\SyliusSchedulerPlugin\Model\ScheduleInterface;
16
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
17
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
18
use Sylius\Component\Resource\Factory\Factory;
19
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
20
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
21
use Symfony\Component\Config\Definition\ConfigurationInterface;
22
use Webmozart\Assert\Assert;
23
24
final class Configuration implements ConfigurationInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getConfigTreeBuilder(): TreeBuilder
30
    {
31
        $treeBuilder = new TreeBuilder();
32
33
        /** @var ArrayNodeDefinition $rootNode */
34
        $rootNode = $treeBuilder->root('setono_sylius_scheduler');
35
36
        $rootNodeChildren = $rootNode->children();
37
        $rootNodeChildren->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM);
38
39
        // Wipe logs in X days after command execution
40
        // Specify 0 to never wipe logs
41
        $rootNodeChildren->scalarNode('wipe_logs_in')->defaultValue(0);
42
43
        // We can specify emails to receive error reports on every Job
44
        // But here we can specify emails which receive error reports for all Jobs
45
        $rootNodeChildren->arrayNode('error_report_emails')
46
            ->treatNullLike([])
47
            ->beforeNormalization()->castToArray()->end()
48
            ->beforeNormalization()
49
                ->always(function (array $emails) {
50
                    foreach ($emails as $email) {
51
                        Assert::true((bool) filter_var($email, FILTER_VALIDATE_EMAIL), sprintf(
52
                            "You should provide valid email. '%s' not looks like valid email.",
53
                            $email
54
                        ));
55
                    }
56
57
                    return $emails;
58
                })
59
            ->end()
60
            ->scalarPrototype()
0 ignored issues
show
Bug introduced by
The method scalarPrototype() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

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

60
            ->/** @scrutinizer ignore-call */ scalarPrototype()
Loading history...
61
            ->cannotBeEmpty()
62
        ;
63
64
        $defaultOptionsNode = $rootNode
65
            ->children()
66
            ->arrayNode('queue_options_defaults')
67
                ->addDefaultsIfNotSet()
68
            ;
69
        $this->addQueueOptions($defaultOptionsNode);
70
71
        $queueOptionsNode = $rootNode
72
            ->children()
73
            ->arrayNode('queue_options')
74
                ->useAttributeAsKey('queue')
75
                ->prototype('array')
76
            ->end()
77
            ;
78
        $this->addQueueOptions($queueOptionsNode);
0 ignored issues
show
Bug introduced by
It seems like $queueOptionsNode can also be of type null; however, parameter $nodeDefinition of Setono\SyliusSchedulerPl...tion::addQueueOptions() does only seem to accept Symfony\Component\Config...der\ArrayNodeDefinition, 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

78
        $this->addQueueOptions(/** @scrutinizer ignore-type */ $queueOptionsNode);
Loading history...
79
80
        $this->addResourcesSection($rootNode);
81
82
        return $treeBuilder;
83
    }
84
85
    /**
86
     * @param ArrayNodeDefinition $node
87
     */
88
    private function addResourcesSection(ArrayNodeDefinition $node): void
89
    {
90
        $node
91
            ->children()
92
                ->arrayNode('resources')
93
                    ->addDefaultsIfNotSet()
94
                    ->children()
95
96
                        ->arrayNode('schedule')
97
                            ->addDefaultsIfNotSet()
98
                            ->children()
99
                                ->variableNode('options')->end()
100
                                ->arrayNode('classes')
0 ignored issues
show
Bug introduced by
The method arrayNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

100
                                ->/** @scrutinizer ignore-call */ arrayNode('classes')
Loading history...
101
                                    ->addDefaultsIfNotSet()
102
                                    ->children()
103
                                        ->scalarNode('model')->defaultValue(Schedule::class)->cannotBeEmpty()->end()
104
                                        ->scalarNode('interface')->defaultValue(ScheduleInterface::class)->cannotBeEmpty()->end()
105
                                        ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
106
                                        ->scalarNode('repository')->defaultValue(ScheduleRepository::class)->cannotBeEmpty()->end()
107
                                        ->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end()
108
                                        ->scalarNode('form')->defaultValue(ScheduleType::class)->cannotBeEmpty()->end()
109
                                    ->end()
110
                                ->end()
111
                            ->end()
112
                        ->end()
113
114
                        ->arrayNode('job')
115
                            ->addDefaultsIfNotSet()
116
                            ->children()
117
                                ->variableNode('options')->end()
118
                                ->arrayNode('classes')
119
                                    ->addDefaultsIfNotSet()
120
                                    ->children()
121
                                        ->scalarNode('model')->defaultValue(Job::class)->cannotBeEmpty()->end()
122
                                        ->scalarNode('interface')->defaultValue(JobInterface::class)->cannotBeEmpty()->end()
123
                                        ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
124
                                        ->scalarNode('repository')->defaultValue(JobRepository::class)->cannotBeEmpty()->end()
125
                                        ->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end()
126
                                    ->end()
127
                                ->end()
128
                            ->end()
129
                        ->end()
130
131
                    ->end()
132
                ->end()
133
            ->end()
134
        ;
135
    }
136
137
    /**
138
     * @param ArrayNodeDefinition $nodeDefinition
139
     */
140
    private function addQueueOptions(ArrayNodeDefinition $nodeDefinition): void
141
    {
142
        $nodeDefinition
143
            ->children()
144
            ->scalarNode('max_concurrent_jobs')->end()
145
        ;
146
    }
147
}
148