Issues (9)

src/Services/ServiceFactory.php (1 issue)

1
<?php
2
3
namespace kalanis\google_maps\Services;
4
5
6
use kalanis\google_maps\Remote\Headers\ApiAuth;
7
use kalanis\google_maps\Remote\Headers\Language;
8
use kalanis\google_maps\ServiceException;
9
use Psr\Http\Message\RequestInterface;
10
use ReflectionClass;
11
use ReflectionException;
12
13
14
/**
15
 * Google Maps PHP Client - factory to get services
16
 **/
17
class ServiceFactory
18
{
19
    /**
20
     * For Client-Service API method director
21
     *
22
     * @var array<string, class-string> Method => Service Class name
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
23
     */
24
    protected array $serviceMethodMap = [
25
        'directions' => Directions::class,
26
        'distanceMatrix' => DistanceMatrix::class,
27
        'elevation' => Elevation::class,
28
        'findPlace' => FindPlace::class,
29
        'findText' => FindText::class,
30
        'geocode' => Geocoding::class,
31
        'reverseGeocode' => Geocoding::class,
32
        'geolocate' => Geolocation::class,
33
        'nearby' => Nearby::class,
34
        'placeDetails' => PlaceDetails::class,
35
        'snapToRoads' => Roads::class,
36
        'computeRoutes' => Routes::class,
37
        'timezone' => Timezone::class,
38
    ];
39
40 12
    public function __construct(
41
        protected readonly RequestInterface $request,
42
        protected readonly ApiAuth          $apiAuth,
43
        protected readonly Language         $lang,
44
    )
45
    {
46 12
    }
47
48
    /**
49
     * @param string|object $method
50
     * @throws ReflectionException
51
     * @throws ServiceException
52
     * @return AbstractService
53
     */
54 12
    public function getService(string|object $method): AbstractService
55
    {
56 12
        if (is_object($method)) {
57 2
            if (!is_a($method, AbstractService::class)) {
58 1
                throw new ServiceException(sprintf('Service *%s* is not an instance of \kalanis\google_maps\Services\AbstractService', $method::class), 400);
59
            }
60 1
            return $method;
61
        }
62
63
        // Matching self::$serviceMethodMap is required
64 10
        if (!isset($this->serviceMethodMap[$method])) {
65 1
            throw new ServiceException(sprintf('Call to undefined service method *%s*', $method), 400);
66
        }
67
68
        // Get the service mapped by method
69 9
        $service = $this->serviceMethodMap[$method];
70
71 9
        $reflection = new ReflectionClass($service);
72 9
        $instance = $reflection->newInstance($this->request, $this->apiAuth, $this->lang);
73
74 9
        if (!$instance instanceof AbstractService) {
75 1
            throw new ServiceException(sprintf('Service *%s* is not an instance of \kalanis\google_maps\Services\AbstractService', $service), 400);
76
        }
77
78 8
        return $instance;
79
    }
80
}
81