1
+ <?php
2
+
3
+ namespace Packlink \BusinessLogic \Controllers ;
4
+
5
+ use Logeecom \Infrastructure \ORM \QueryFilter \Operators ;
6
+ use Logeecom \Infrastructure \ORM \QueryFilter \QueryFilter ;
7
+ use Logeecom \Infrastructure \ORM \RepositoryRegistry ;
8
+ use Logeecom \Infrastructure \ServiceRegister ;
9
+ use Logeecom \Infrastructure \TaskExecution \Exceptions \QueueStorageUnavailableException ;
10
+ use Logeecom \Infrastructure \TaskExecution \QueueItem ;
11
+ use Logeecom \Infrastructure \TaskExecution \QueueService ;
12
+ use Packlink \BusinessLogic \Configuration ;
13
+ use Packlink \BusinessLogic \Controllers \DTO \TaskStatus ;
14
+ use Packlink \BusinessLogic \Tasks \UpdateShippingServicesTask ;
15
+
16
+ class ManualRefreshController
17
+ {
18
+ /**
19
+ * Configuration service instance.
20
+ *
21
+ * @var Configuration
22
+ */
23
+ protected $ configuration ;
24
+
25
+ /**
26
+ * Enqueues the UpdateShippingServicesTask and returns a JSON response.
27
+ *
28
+ * @return TaskStatus
29
+ */
30
+ public function enqueueUpdateTask ()
31
+ {
32
+ $ queueService = ServiceRegister::getService (QueueService::CLASS_NAME );
33
+
34
+ $ configService = $ this ->getConfigService ();
35
+
36
+ try {
37
+ $ queueService ->enqueue (
38
+ $ configService ->getDefaultQueueName (),
39
+ new UpdateShippingServicesTask (),
40
+ $ configService ->getContext ()
41
+ );
42
+
43
+ $ taskStatus = new TaskStatus ();
44
+ $ taskStatus ->status = TaskStatus::SUCCESS ;
45
+ $ taskStatus ->message = 'Task successfully enqueued. ' ;
46
+ return $ taskStatus ;
47
+
48
+ } catch (QueueStorageUnavailableException $ e ) {
49
+ $ taskStatus = new TaskStatus ();
50
+ $ taskStatus ->status = TaskStatus::ERROR ;
51
+ $ taskStatus ->message = 'Failed to enqueue task: ' . $ e ->getMessage ();
52
+ return $ taskStatus ;
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Checks the status of the task responsible for getting services.
58
+ *
59
+ * @param string $context
60
+ *
61
+ * @return TaskStatus <p>One of the following statuses:
62
+ * QueueItem::FAILED - when the task failed,
63
+ * QueueItem::COMPLETED - when the task completed successfully,
64
+ * QueueItem::IN_PROGRESS - when the task is in progress,
65
+ * QueueItem::QUEUED - when the default warehouse is not set by user and the task was not enqueued.
66
+ * </p>
67
+ *
68
+ * @throws \Logeecom\Infrastructure\ORM\Exceptions\QueryFilterInvalidParamException
69
+ * @throws \Logeecom\Infrastructure\ORM\Exceptions\RepositoryClassException
70
+ * @throws \Logeecom\Infrastructure\ORM\Exceptions\RepositoryNotRegisteredException
71
+ * @throws \Logeecom\Infrastructure\TaskExecution\Exceptions\QueueItemDeserializationException
72
+ */
73
+ public function getTaskStatus ($ context = '' )
74
+ {
75
+ /**@var QueueService $service */
76
+ $ service = ServiceRegister::getService (QueueService::CLASS_NAME );
77
+
78
+ $ item = $ service ->findLatestByType ('UpdateShippingServicesTask ' , $ context );
79
+
80
+ $ taskStatus = new TaskStatus ();
81
+
82
+ if ($ item ) {
83
+ $ status = $ item ->getStatus ();
84
+ $ taskStatus ->status = $ status ;
85
+
86
+ if ($ status === QueueItem::FAILED ) {
87
+ $ taskStatus ->message = $ item ->getFailureDescription ();
88
+ }
89
+
90
+ if ($ status === QueueItem::COMPLETED ) {
91
+ $ taskStatus ->message = 'Queue item completed ' ;
92
+ }
93
+
94
+ return $ taskStatus ;
95
+ }
96
+
97
+ $ taskStatus ->status = QueueItem::CREATED ;
98
+ $ taskStatus ->message = 'Queue item not found. ' ;
99
+
100
+ return $ taskStatus ;
101
+ }
102
+
103
+ /**
104
+ * Returns an instance of configuration service.
105
+ *
106
+ * @return \Packlink\BusinessLogic\Configuration Configuration service.
107
+ */
108
+ protected function getConfigService ()
109
+ {
110
+ if ($ this ->configuration === null ) {
111
+ $ this ->configuration = ServiceRegister::getService (Configuration::CLASS_NAME );
112
+ }
113
+
114
+ return $ this ->configuration ;
115
+ }
116
+
117
+ }
0 commit comments