GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

BookingService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
/*
3
 * This file is part of the prooph/php-ddd-cargo-sample.
4
 * (c) Alexander Miertsch <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 * 
9
 * Date: 29.03.14 - 17:55
10
 */
11
declare(strict_types = 1);
12
13
namespace Codeliner\CargoBackend\Application\Booking;
14
15
use Codeliner\CargoBackend\Application\Booking\Assembler\CargoRoutingDtoAssembler;
16
use Codeliner\CargoBackend\Application\Booking\Assembler\RouteCandidateDtoAssembler;
17
use Codeliner\CargoBackend\Application\Booking\Dto\CargoRoutingDto;
18
use Codeliner\CargoBackend\Application\Booking\Dto\LocationDto;
19
use Codeliner\CargoBackend\Application\Booking\Dto\RouteCandidateDto;
20
use Codeliner\CargoBackend\Application\Exception\CargoNotFoundException;
21
use Codeliner\CargoBackend\Infrastructure\Persistence\Transaction\TransactionManager;
22
use Codeliner\CargoBackend\Model\Cargo\Cargo;
23
use Codeliner\CargoBackend\Model\Cargo\CargoRepositoryInterface;
24
use Codeliner\CargoBackend\Model\Cargo\RouteSpecification;
25
use Codeliner\CargoBackend\Model\Cargo\TrackingId;
26
use Codeliner\CargoBackend\Model\Routing\RoutingServiceInterface;
27
28
/**
29
 * Class BookingService
30
 *
31
 * @package Codeliner\CargoBackend\Application\Booking
32
 * @author Alexander Miertsch <[email protected]>
33
 */
34
class BookingService
35
{
36
    /**
37
     * @var CargoRepositoryInterface
38
     */
39
    private $cargoRepository;
40
41
    /**
42
     * @var TransactionManager
43
     */
44
    private $transactionManager;
45
46
    /**
47
     * @var RoutingServiceInterface
48
     */
49
    private $routingService;
50
51
    /**
52
     * @var array List of locations
53
     */
54
    private $locations;
55
56
    /**
57
     * @param CargoRepositoryInterface $aCargoRepository
58
     * @param TransactionManager $aTransactionManager
59
     * @param RoutingServiceInterface $aRoutingService
60
     * @param array $locations
61
     */
62
    public function __construct(
63
        CargoRepositoryInterface $aCargoRepository,
64
        TransactionManager $aTransactionManager,
65
        RoutingServiceInterface $aRoutingService,
66
        array $locations
67
    ) {
68
        $this->cargoRepository    = $aCargoRepository;
69
        $this->transactionManager = $aTransactionManager;
70
        $this->routingService     = $aRoutingService;
71
        $this->locations          = $locations;
72
    }
73
74
    /**
75
     * @param string $anOrigin
76
     * @param string $aDestination
77
     * @throws \Exception If booking fails
78
     * @return string
79
     */
80
    public function bookNewCargo(string $anOrigin, string $aDestination): string
81
    {
82
        $trackingId = $this->cargoRepository->getNextTrackingId();
83
84
        $routeSpecification = new RouteSpecification($anOrigin, $aDestination);
85
86
        $cargo = new Cargo($trackingId, $routeSpecification);
87
88
        $this->transactionManager->beginTransaction();
89
90
        try {
91
            $this->cargoRepository->store($cargo);
92
93
            $this->transactionManager->commit();
94
95
            return $trackingId->toString();
96
        } catch (\Exception $ex) {
97
            $this->transactionManager->rollback();
98
99
            throw $ex;
100
        }
101
    }
102
103
    /**
104
     * @param string $aTrackingId
105
     * @throws \Codeliner\CargoBackend\Application\Exception\CargoNotFoundException
106
     * @return CargoRoutingDto
107
     */
108
    public function loadCargoForRouting(string $aTrackingId): CargoRoutingDto
109
    {
110
        $aTrackingId = TrackingId::fromString($aTrackingId);
111
112
        $cargo = $this->cargoRepository->get($aTrackingId);
113
114
        if (! $cargo) {
115
            throw CargoNotFoundException::forTrackingId($aTrackingId);
116
        }
117
118
        $cargoRoutingDtoAssembler = new CargoRoutingDtoAssembler();
119
120
        return $cargoRoutingDtoAssembler->toDto($cargo);
121
    }
122
123
    /**
124
     * @param string $aTrackingId
125
     * @throws CargoNotFoundException
126
     * @return RouteCandidateDto[]
127
     */
128
    public function requestPossibleRoutesForCargo(string $aTrackingId): array
129
    {
130
        $aTrackingId = TrackingId::fromString($aTrackingId);
131
132
        $cargo = $this->cargoRepository->get($aTrackingId);
133
134
        if (! $cargo) {
135
            throw CargoNotFoundException::forTrackingId($aTrackingId);
136
        }
137
138
        $itineraries = $this->routingService->fetchRoutesForSpecification($cargo->routeSpecification());
139
140
        $routeCandidates = array();
141
        $routeCandidateAssembler = new RouteCandidateDtoAssembler();
142
143
        foreach ($itineraries as $itinerary) {
144
            $routeCandidates[] = $routeCandidateAssembler->toDto($itinerary);
145
        }
146
147
        return $routeCandidates;
148
    }
149
150
    /**
151
     * @param string $aTrackingId
152
     * @param RouteCandidateDto $aRoute
153
     * @throws CargoNotFoundException
154
     * @throws \Exception
155
     * @return void
156
     */
157
    public function assignCargoToRoute(string $aTrackingId, RouteCandidateDto $aRoute)
158
    {
159
        $aTrackingId = TrackingId::fromString($aTrackingId);
160
        $cargo = $this->cargoRepository->get($aTrackingId);
161
162
        if (! $cargo) {
163
            throw CargoNotFoundException::forTrackingId($aTrackingId);
164
        }
165
166
        $routeCandidateAssembler = new RouteCandidateDtoAssembler();
167
168
        $itinerary = $routeCandidateAssembler->toItinerary($aRoute);
169
170
        $this->transactionManager->beginTransaction();
171
172
        try {
173
174
            $cargo->assignToRoute($itinerary);
175
176
            $this->cargoRepository->store($cargo);
177
178
            $this->transactionManager->commit();
179
180
        } catch (\Exception $ex) {
181
182
            $this->transactionManager->rollback();
183
184
            throw $ex;
185
        }
186
    }
187
188
    /**
189
     * @return LocationDto[]
190
     */
191
    public function listShippingLocations(): array
192
    {
193
        $locationDtos = array();
194
195
        foreach ($this->locations as $unLocode => $name) {
196
            $locationDto = new LocationDto();
197
            $locationDto->setUnLocode($unLocode);
198
            $locationDto->setName($name);
199
200
            $locationDtos[] = $locationDto;
201
        }
202
203
        return $locationDtos;
204
    }
205
206
    /**
207
     * @return CargoRoutingDto[]
208
     */
209
    public function listAllCargos(): array
210
    {
211
        $cargos = $this->cargoRepository->getAll();
212
213
        $cargoRoutingDtos = array();
214
215
        $cargoRoutingDtoAssembler = new CargoRoutingDtoAssembler();
216
217
        foreach ($cargos as $cargo) {
218
            $cargoRoutingDtos[] = $cargoRoutingDtoAssembler->toDto($cargo);
219
        }
220
221
        return $cargoRoutingDtos;
222
    }
223
}
224