Completed
Push — master ( 8edee4...0135c2 )
by Marin
04:35
created

Task::setPickupTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Anorgan\Onfleet;
4
5
use Symfony\Component\Console\Exception\LogicException;
6
7
/**
8
 * Class Task
9
 * @package Anorgan\Onfleet
10
 */
11
class Task extends Entity
12
{
13
    /**
14
     * Unassigned: task has not yet been assigned to a worker.
15
     */
16
    const STATE_UNASSIGNED = 0;
17
18
    /**
19
     * Assigned: task has been assigned to a worker.
20
     */
21
    const STATE_ASSIGNED = 1;
22
23
    /**
24
     * Active: task has been started by its assigned worker.
25
     */
26
    const STATE_ACTIVE = 2;
27
28
    /**
29
     * Completed: task has been completed by its assigned worker.
30
     */
31
    const STATE_COMPLETED = 3;
32
33
    /**
34
     * The distance mode finds the active worker whose last-known location is closest to the task's destination.
35
     */
36
    const AUTO_ASSIGN_MODE_DISTANCE = 'distance';
37
38
    /**
39
     * The worker who has to travel the shortest distance is considered to have the lightest load,
40
     * and as such will be assigned the task being created.
41
     */
42
    const AUTO_ASSIGN_MODE_LOAD = 'load';
43
44
    const CONTAINER_TYPE_TEAM   = 'TEAM';
45
    const CONTAINER_TYPE_WORKER = 'WORKER';
46
47
    protected $organization;
48
    protected $shortId;
49
    protected $trackingURL;
50
    protected $worker;
51
    protected $merchant;
52
    protected $executor;
53
    protected $creator;
54
    protected $dependencies = [];
55
    protected $state        = self::STATE_UNASSIGNED;
56
    protected $completeAfter;
57
    protected $completeBefore;
58
    protected $pickupTask = false;
59
    protected $notes;
60
    protected $completionDetails = [];
61
    protected $feedback          = [];
62
    protected $metadata          = [];
63
    protected $overrides         = [];
64
    protected $container         = [];
65
    protected $recipients        = [];
66
    protected $destination;
67
    protected $delayTime;
68
    protected $timeCreated;
69
    protected $timeLastModified;
70
    protected $didAutoAssign;
71
72
    protected $endpoint = 'tasks';
73
74
    protected static $properties = [
75
        'id',
76
        'organization',
77
        'shortId',
78
        'trackingURL',
79
        'worker',
80
        'merchant',
81
        'executor',
82
        'creator',
83
        'dependencies',
84
        'state',
85
        'completeAfter',
86
        'completeBefore',
87
        'pickupTask',
88
        'notes',
89
        'completionDetails',
90
        'feedback',
91
        'metadata',
92
        'overrides',
93
        'container',
94
        'recipients',
95
        'destination',
96
        'delayTime',
97
        'timeCreated',
98
        'timeLastModified',
99
        'didAutoAssign',
100
    ];
101
102
    /**
103
     * @return string
104
     */
105
    public function getOrganization()
106
    {
107
        return $this->organization;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getShortId()
114
    {
115
        return $this->shortId;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getTrackingURL()
122
    {
123
        return $this->trackingURL;
124
    }
125
126
    /**
127
     * @return string ID of the worker. This properly will be completely removed from non-completed tasks in favor of
128
     *                                  container in future versions of the API.
129
     */
130
    public function getWorker()
131
    {
132
        return $this->worker;
133
    }
134
135
    /**
136
     * @return string|null
137
     */
138
    public function getMerchant()
139
    {
140
        return $this->merchant;
141
    }
142
143
    /**
144
     * @param string $merchant
145
     */
146
    public function setMerchant($merchant)
147
    {
148
        $this->merchant = $merchant;
149
    }
150
151
    /**
152
     * @return string|null
153
     */
154
    public function getExecutor()
155
    {
156
        return $this->executor;
157
    }
158
159
    /**
160
     * @param string $executor
161
     */
162
    public function setExecutor($executor)
163
    {
164
        $this->executor = $executor;
165
    }
166
167
    /**
168
     * @return string ID of creator
169
     */
170
    public function getCreator()
171
    {
172
        return $this->creator;
173
    }
174
175
    /**
176
     * @return array
177
     */
178
    public function getDependencies(): array
179
    {
180
        return $this->dependencies;
181
    }
182
183
    /**
184
     * @param array $dependencies
185
     */
186
    public function setDependencies(array $dependencies = null)
187
    {
188
        $this->dependencies = $dependencies;
189
    }
190
191
    /**
192
     * @return int
193
     */
194
    public function getState(): int
195
    {
196
        return $this->state;
197
    }
198
199
    /**
200
     * @param int $state
201
     */
202
    public function setState(int $state)
203
    {
204
        $this->state = $state;
205
    }
206
207
    /**
208
     * @return int Amount of time in seconds that a task is delayed by.
209
     */
210
    public function getDelayTime()
211
    {
212
        return $this->delayTime;
213
    }
214
215
    /**
216
     * @return \DateTime
217
     */
218
    public function getCompleteAfter()
219
    {
220
        return $this->toDateTime($this->completeAfter);
221
    }
222
223
    /**
224
     * @param int|\DateTime $completeAfter
225
     */
226
    public function setCompleteAfter($completeAfter)
227
    {
228
        $this->completeAfter = $this->toTimestamp($completeAfter);
229
    }
230
231
    /**
232
     * @return \DateTime
233
     */
234
    public function getCompleteBefore()
235
    {
236
        return $this->toDateTime($this->completeBefore);
237
    }
238
239
    /**
240
     * @param int|\DateTime $completeBefore
241
     */
242
    public function setCompleteBefore($completeBefore)
243
    {
244
        $this->completeBefore = $this->toTimestamp($completeBefore);
245
    }
246
247
    /**
248
     * @return boolean
249
     */
250
    public function isPickupTask(): bool
251
    {
252
        return $this->pickupTask;
253
    }
254
255
    /**
256
     * @param boolean $pickupTask
257
     */
258
    public function setPickupTask($pickupTask)
259
    {
260
        $this->pickupTask = $pickupTask;
261
    }
262
263
    /**
264
     * @return string
265
     */
266
    public function getNotes()
267
    {
268
        return $this->notes;
269
    }
270
271
    /**
272
     * @param string $notes
273
     */
274
    public function setNotes($notes)
275
    {
276
        $this->notes = $notes;
277
    }
278
279
    /**
280
     * @return array
281
     */
282
    public function getCompletionDetails(): array
283
    {
284
        return $this->completionDetails;
285
    }
286
287
    /**
288
     * @param array $completionDetails
289
     */
290
    public function setCompletionDetails(array $completionDetails)
291
    {
292
        $this->completionDetails = $completionDetails;
293
    }
294
295
    /**
296
     * @return array
297
     */
298
    public function getFeedback(): array
299
    {
300
        return $this->feedback;
301
    }
302
303
    /**
304
     * @return array
305
     */
306
    public function getMetadata(): array
307
    {
308
        return $this->metadata;
309
    }
310
311
    /**
312
     * @return array
313
     */
314
    public function getOverrides(): array
315
    {
316
        return $this->overrides;
317
    }
318
319
    /**
320
     * @param array $overrides
321
     */
322
    public function setOverrides(array $overrides)
323
    {
324
        $this->overrides = $overrides;
325
    }
326
327
    /**
328
     * @return array
329
     */
330
    public function getContainer(): array
331
    {
332
        return $this->container;
333
    }
334
335
    /**
336
     * @param array $container
337
     */
338
    public function setContainer(array $container)
339
    {
340
        $this->container = $container;
341
    }
342
343
    /**
344
     * @return array
345
     */
346
    public function getRecipients(): array
347
    {
348
        return $this->recipients;
349
    }
350
351
    /**
352
     * @param array $recipients
353
     */
354
    public function setRecipients(array $recipients)
355
    {
356
        $this->recipients = $recipients;
357
    }
358
359
    /**
360
     * @return string
361
     */
362
    public function getDestination()
363
    {
364
        return $this->destination;
365
    }
366
367
    /**
368
     * @param string $destination
369
     */
370
    public function setDestination($destination)
371
    {
372
        $this->destination = $destination;
373
    }
374
375
    /**
376
     * @return \DateTime
377
     */
378
    public function getTimeCreated()
379
    {
380
        return $this->toDateTime($this->timeCreated);
381
    }
382
383
    /**
384
     * @return \DateTime
385
     */
386
    public function getTimeLastModified()
387
    {
388
        return $this->toDateTime($this->timeLastModified);
389
    }
390
391
    /**
392
     * Available only on creation, and if using auto assign feature. If we fail to find a suitable worker because of an
393
     * error or any other unexpected condition, didAutoAssign will be false.
394
     * @return boolean
395
     */
396
    public function isAutoAssigned()
397
    {
398
        return $this->didAutoAssign;
399
    }
400
401
    /**
402
     * @throws \LogicException
403
     * @return Entity
404
     */
405
    public function update()
406
    {
407
        if (self::STATE_COMPLETED === $this->getState()) {
408
            throw new LogicException('Unable to modify completed task');
409
        }
410
411
        return parent::update();
412
    }
413
414
    /**
415
     * @throws \LogicException
416
     */
417
    public function delete()
418
    {
419
        if (self::STATE_COMPLETED === $this->getState()) {
420
            throw new LogicException('Unable to delete completed task');
421
        }
422
423
        if (self::STATE_ACTIVE === $this->getState()) {
424
            throw new LogicException('Unable to delete active task');
425
        }
426
427
        parent::delete();
428
    }
429
430
    /**
431
     * @param bool $success
432
     * @param string $notes
433
     */
434
    public function complete($success = true, $notes = null)
435
    {
436
        if (self::STATE_ACTIVE !== $this->getState()) {
437
            throw new LogicException('Unable to complete non active task');
438
        }
439
        $this->client->post($this->endpoint .'/'. $this->id .'/complete', [
440
            'json' => [
441
                'completionDetails' => [
442
                    'success' => $success,
443
                    'notes'   => $notes
444
                ]
445
            ]
446
        ]);
447
448
        $this->state = self::STATE_COMPLETED;
449
    }
450
451
452
    /**
453
     * @param Destination $destination           The valid Destination object.
454
     * @param Recipient|Recipient[] $recipients  A valid array of zero or one Recipient objects.
455
     * @param Organization $merchant             Optional. The organization that will be displayed to the
456
     *                                           recipient of the task. Defaults to the creating organization.
457
     *                                           If you perform deliveries on behalf of a connected organization and
458
     *                                           want to display their name, logo, and branded notifications,
459
     *                                           provide their organization.
460
     * @param Organization $executor             Optional. The organization that will be responsible for fulfilling the
461
     *                                           task. Defaults to the creating organization. If you delegate your
462
     *                                           deliveries to a third party, provide their organization.
463
     * @param string $notes                        Optional. Notes for the task, e.g. "Order 332: 10 x Soup de Jour"
464
     * @param string $autoAssignMode             The desired automatic assignment mode. Either distance or load.
465
     * @param Team $autoAssignTeam               Optional. The team from which to pick the workers to consider for
466
     *                                           automatic assignment.
467
     * @param int|\DateTime $completeAfter                 Optional. A timestamp for the earliest time the task should be completed.
468
     * @param int|\DateTime $completeBefore                Optional. A timestamp for the latest time the task should be completed.
469
     * @param bool $pickupTask                   Optional. Whether the task is a pickup task.
470
     * @param array|null $dependencies           Optional. One or more IDs of tasks which must be completed prior to this task.
471
     * @param int $quantity                      Optional. The number of units to be dropped off while completing this
472
     *                                           task, for route optimization purposes.
473
     * @param int $serviceTime                   Optional. The number of minutes to be spent by the worker on arrival at
474
     *                                           this task's destination, for route optimization purposes.
475
     * @return array
476
     */
477
    public static function createAutoAssignedArray(
478
        Destination $destination,
479
        $recipients,
480
        Organization $merchant = null,
481
        Organization $executor = null,
482
        $notes = null,
483
        $autoAssignMode = self::AUTO_ASSIGN_MODE_LOAD,
484
        Team $autoAssignTeam = null,
485
        $completeAfter = null,
486
        $completeBefore = null,
487
        $pickupTask = false,
488
        $dependencies = null,
489
        $quantity = null,
490
        $serviceTime = null
491
    ): array {
492
        $task = new static(new Client(null));
493
494
        if ($recipients instanceof Recipient) {
495
            $recipients = [$recipients->getId()];
496
        }
497
        $task->setRecipients($recipients);
498
499
        if ($destination instanceof Destination) {
500
            $destination = $destination->getId();
501
        }
502
        $task->setDestination($destination);
503
504
        if (null !== $merchant) {
505
            $task->setMerchant($merchant->getId());
506
        }
507
508
        if (null !== $executor) {
509
            $task->setExecutor($executor->getId());
510
        }
511
512
        $task->setNotes($notes);
513
        $task->setCompleteAfter($completeAfter);
0 ignored issues
show
Bug introduced by
It seems like $completeAfter defined by parameter $completeAfter on line 485 can also be of type null; however, Anorgan\Onfleet\Task::setCompleteAfter() does only seem to accept integer|object<DateTime>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
514
        $task->setCompleteBefore($completeBefore);
0 ignored issues
show
Bug introduced by
It seems like $completeBefore defined by parameter $completeBefore on line 486 can also be of type null; however, Anorgan\Onfleet\Task::setCompleteBefore() does only seem to accept integer|object<DateTime>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
515
        $task->setPickupTask($pickupTask);
516
        $task->setDependencies($dependencies);
517
518
        $taskArray = $task->toArray();
519
520
        $taskArray['quantity']    = $quantity;
521
        $taskArray['serviceTime'] = $serviceTime;
522
523
        $taskArray['autoAssign'] = [
524
            'mode' => $autoAssignMode
525
        ];
526
527
        if (null !== $autoAssignTeam) {
528
            $taskArray['autoAssign']['team'] = $autoAssignTeam->getId();
529
        }
530
531
        return array_filter($taskArray);
532
    }
533
534
    /**
535
     * @param Destination $destination           The valid Destination object.
536
     * @param Recipient|Recipient[] $recipients  A valid array of zero or one Recipient objects.
537
     * @param Organization $merchant             Optional. The organization that will be displayed to the
538
     *                                           recipient of the task. Defaults to the creating organization.
539
     *                                           If you perform deliveries on behalf of a connected organization and
540
     *                                           want to display their name, logo, and branded notifications,
541
     *                                           provide their organization.
542
     * @param Organization $executor             Optional. The organization that will be responsible for fulfilling the
543
     *                                           task. Defaults to the creating organization. If you delegate your
544
     *                                           deliveries to a third party, provide their organization.
545
     * @param string $notes                        Optional. Notes for the task, e.g. "Order 332: 10 x Soup de Jour"
546
     * @param string $containerType              The type of the container to which the task is to be assigned.
547
     * @param string $containerTarget            ID of the target team or worker.
548
     * @param int $completeAfter                 Optional. A timestamp for the earliest time the task should be completed.
549
     * @param int $completeBefore                Optional. A timestamp for the latest time the task should be completed.
550
     * @param bool $pickupTask                   Optional. Whether the task is a pickup task.
551
     * @param array $dependencies                Optional. One or more IDs of tasks which must be completed prior to this task.
552
     * @param int $quantity                      Optional. The number of units to be dropped off while completing this
553
     *                                           task, for route optimization purposes.
554
     * @param int $serviceTime                   Optional. The number of minutes to be spent by the worker on arrival at
555
     *                                           this task's destination, for route optimization purposes.
556
     * @return array
557
     */
558
    public static function createManualAssignedArray(
559
        Destination $destination,
560
        $recipients,
561
        Organization $merchant = null,
562
        Organization $executor = null,
563
        $notes = null,
564
        $containerType = self::CONTAINER_TYPE_TEAM,
565
        $containerTarget,
566
        $completeAfter = null,
567
        $completeBefore = null,
568
        $pickupTask = false,
569
        $dependencies = null,
570
        $quantity = null,
571
        $serviceTime = null
572
    ): array {
573
        $task = new static(new Client(null));
574
575
        if ($recipients instanceof Recipient) {
576
            $recipients = [$recipients->getId()];
577
        }
578
        $task->setRecipients($recipients);
579
580
        if ($destination instanceof Destination) {
581
            $destination = $destination->getId();
582
        }
583
        $task->setDestination($destination);
584
585
        if (null !== $merchant) {
586
            $task->setMerchant($merchant->getId());
587
        }
588
589
        if (null !== $executor) {
590
            $task->setExecutor($executor->getId());
591
        }
592
593
        $task->setNotes($notes);
594
        $task->setCompleteAfter($completeAfter);
595
        $task->setCompleteBefore($completeBefore);
596
        $task->setPickupTask($pickupTask);
597
        $task->setDependencies($dependencies);
598
599
        $taskArray = $task->toArray();
600
601
        $taskArray['quantity']    = $quantity;
602
        $taskArray['serviceTime'] = $serviceTime;
603
604
        $taskArray['container'] = [
605
            'type'                     => $containerType,
606
            strtolower($containerType) => $containerTarget
607
        ];
608
609
        return array_filter($taskArray);
610
    }
611
}
612