CommandScheduleRepository::getAll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Tkotosz\CommandScheduler\Model;
4
5
use Exception;
6
use Tkotosz\CommandScheduler\Api\CommandScheduleRepositoryInterface;
7
use Tkotosz\CommandScheduler\Api\Data\CommandScheduleInterface;
8
use Tkotosz\CommandScheduler\Api\Data\CommandScheduleInterfaceFactory as CommandScheduleFactory;
0 ignored issues
show
Bug introduced by
The type Tkotosz\CommandScheduler...cheduleInterfaceFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Tkotosz\CommandScheduler\Api\Data\CommandScheduleSearchResultsInterface;
10
use Tkotosz\CommandScheduler\Api\Data\CommandScheduleSearchResultsInterfaceFactory as CommandScheduleSearchResultsFactory;
0 ignored issues
show
Bug introduced by
The type Tkotosz\CommandScheduler...ResultsInterfaceFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Tkotosz\CommandScheduler\Model\ResourceModel\CommandSchedule as CommandScheduleResource;
12
use Tkotosz\CommandScheduler\Model\ResourceModel\CommandSchedule\CollectionFactory as CommandScheduleCollectionFactory;
0 ignored issues
show
Bug introduced by
The type Tkotosz\CommandScheduler...edule\CollectionFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Magento\Framework\Api\SearchCriteria;
14
use Magento\Framework\Api\SearchCriteriaBuilder;
15
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
16
use Magento\Framework\Exception\CouldNotDeleteException;
17
use Magento\Framework\Exception\CouldNotSaveException;
18
use Magento\Framework\Exception\NoSuchEntityException;
19
20
class CommandScheduleRepository implements CommandScheduleRepositoryInterface
21
{
22
    /**
23
     * @var CommandScheduleFactory
24
     */
25
    private $commandScheduleFactory;
26
    
27
    /**
28
     * @var CommandScheduleResource
29
     */
30
    private $commandScheduleResource;
31
    
32
    /**
33
     * @var CommandScheduleCollectionFactory
34
     */
35
    private $commandScheduleCollectionFactory;
36
    
37
    /**
38
     * @var CollectionProcessorInterface
39
     */
40
    private $collectionProcessor;
41
    
42
    /**
43
     * @var CommandScheduleSearchResultsFactory
44
     */
45
    private $searchResultsFactory;
46
    
47
    /**
48
     * @var SearchCriteriaBuilder
49
     */
50
    private $searchCriteriaBuilder;
51
    
52
    /**
53
     * @param CommandScheduleFactory              $commandScheduleFactory
54
     * @param CommandScheduleResource             $commandScheduleResource
55
     * @param CommandScheduleCollectionFactory    $commandScheduleCollectionFactory
56
     * @param CollectionProcessorInterface        $collectionProcessor
57
     * @param CommandScheduleSearchResultsFactory $searchResultsFactory
58
     * @param SearchCriteriaBuilder               $searchCriteriaBuilder
59
     */
60
    public function __construct(
61
        CommandScheduleFactory $commandScheduleFactory,
62
        CommandScheduleResource $commandScheduleResource,
63
        CommandScheduleCollectionFactory $commandScheduleCollectionFactory,
64
        CollectionProcessorInterface $collectionProcessor,
65
        CommandScheduleSearchResultsFactory $searchResultsFactory,
66
        SearchCriteriaBuilder $searchCriteriaBuilder
67
    ) {
68
        $this->commandScheduleFactory = $commandScheduleFactory;
69
        $this->commandScheduleResource = $commandScheduleResource;
70
        $this->commandScheduleCollectionFactory = $commandScheduleCollectionFactory;
71
        $this->collectionProcessor = $collectionProcessor;
72
        $this->searchResultsFactory = $searchResultsFactory;
73
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
74
    }
75
76
    public function save(CommandScheduleInterface $commandSchedule): CommandScheduleInterface
77
    {
78
        try {
79
            $this->commandScheduleResource->save($commandSchedule);
80
        } catch (Exception $e) {
81
            throw new CouldNotSaveException(__('Could not save the command schedule: %1', $e->getMessage()), $e);
82
        }
83
84
        return $commandSchedule;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $commandSchedule returns the type Magento\Framework\Model\AbstractModel which is incompatible with the type-hinted return Tkotosz\CommandScheduler...ommandScheduleInterface.
Loading history...
85
    }
86
87
    public function create(string $commandName, string $commandParams): CommandScheduleInterface
88
    {
89
        $command = $this->commandScheduleFactory->create()
90
            ->setCommandName($commandName)
91
            ->setCommandParams($commandParams)
92
            ->setStatus('pending');
93
94
        return $this->save($command);
95
    }
96
97
    public function updateStatus(CommandScheduleInterface $commandSchedule, string $status): CommandScheduleInterface
98
    {
99
        return $this->save($commandSchedule->setStatus($status));
100
    }
101
102
    public function updateResult(CommandScheduleInterface $commandSchedule, string $result): CommandScheduleInterface
103
    {
104
        return $this->save($commandSchedule->setResult($result));
105
    }
106
107
    public function getById(int $id): CommandScheduleInterface
108
    {
109
        $commandSchedule = $this->commandScheduleFactory->create();
110
111
        $this->commandScheduleResource->load($commandSchedule, $id);
112
        
113
        if (!$commandSchedule->getId()) {
114
            throw new NoSuchEntityException(__("Command Schedule with id '%1' not found", $id));
115
        }
116
117
        return $commandSchedule;
118
    }
119
120
    public function getList(SearchCriteria $searchCriteria): CommandScheduleSearchResultsInterface
121
    {
122
        $collection = $this->commandScheduleCollectionFactory->create();
123
        
124
        $this->collectionProcessor->process($searchCriteria, $collection);
125
        
126
        $searchResults = $this->searchResultsFactory->create();
127
        $searchResults->setSearchCriteria($searchCriteria);
128
        $searchResults->setItems($collection->getItems());
129
        $searchResults->setTotalCount($collection->getSize());
130
131
        return $searchResults;
132
    }
133
134
    public function getOneByStatus(string $status): ?CommandScheduleInterface
135
    {
136
        $schedule = $this->commandScheduleCollectionFactory->create()
137
            ->addFieldToFilter('status', $status)
138
            ->getFirstItem();
139
140
        return $schedule->getId() ? $schedule : null;
141
    }
142
143
    public function getAll(): CommandScheduleSearchResultsInterface
144
    {
145
        return $this->getList($this->searchCriteriaBuilder->create());
146
    }
147
148
    public function delete(CommandScheduleInterface $commandSchedule): bool
149
    {
150
        try {
151
            $this->commandScheduleResource->delete($commandSchedule);
152
        } catch (Exception $e) {
153
            throw new CouldNotDeleteException(__('Could not delete the command schedule: %1', $e->getMessage()));
154
        }
155
156
        return true;
157
    }
158
159
    public function deleteById(int $id): bool
160
    {
161
        return $this->delete($this->getById($id));
162
    }
163
}
164