ScheduleFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createNew() 0 3 1
A createForCommand() 0 18 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusSchedulerPlugin\Factory;
6
7
use Setono\SyliusSchedulerPlugin\Model\ScheduleInterface;
8
use Sylius\Component\Core\Formatter\StringInflector;
9
use Sylius\Component\Resource\Factory\FactoryInterface;
10
11
class ScheduleFactory implements ScheduleFactoryInterface
12
{
13
    /**
14
     * @var FactoryInterface
15
     */
16
    private $factory;
17
18
    /**
19
     * @param FactoryInterface $factory
20
     */
21
    public function __construct(
22
        FactoryInterface $factory
23
    ) {
24
        $this->factory = $factory;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function createNew()
31
    {
32
        return $this->factory->createNew();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function createForCommand(string $command, array $args = [], ?string $scheduleName = null, ?string $scheduleCode = null): ScheduleInterface
39
    {
40
        /** @var ScheduleInterface $schedule */
41
        $schedule = $this->createNew();
42
        if (null === $scheduleName) {
43
            $scheduleName = $command;
44
        }
45
        if (null === $scheduleCode) {
46
            $scheduleCode = StringInflector::nameToCode(
47
                \strtolower($scheduleName)
48
            );
49
        }
50
        $schedule->setName($scheduleName);
51
        $schedule->setCode($scheduleCode);
52
        $schedule->setCommand($command);
53
        $schedule->setArgs($args);
54
55
        return $schedule;
56
    }
57
}
58