DriversFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 0
cbo 2
dl 0
loc 35
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A make() 0 18 3
1
<?php
2
3
namespace Vinelab\UrlShortener\Base;
4
5
use Vinelab\UrlShortener\Exceptions\MissingConfigurationException;
6
use Vinelab\UrlShortener\Exceptions\UnsupportedDriverException;
7
8
/**
9
 * Responsible of initializing objects based on the default driver name.
10
 *
11
 * The class can be accessed statically or normally
12
 * static example: $driver = DriversFactory::make($driverName, $driverParameters);
13
 * normal example: $driver = (new DriversFactory())->make($driverName, $driverParameters);
14
 *
15
 * @category Factory Class (for Drivers)
16
 *
17
 * @author   Mahmoud Zalt <[email protected]>
18
 */
19
class DriversFactory
20
{
21
    /**
22
     * the namespace of the drivers.
23
     */
24
    const DRIVERS_NAMESPACE = 'Vinelab\\UrlShortener\\Drivers\\';
25
26
    /**
27
     * initialize the driver instance and return it.
28
     *
29
     * @param $name       driver name to be initialized
30
     * @param $parameters parameters to be passed to the driver constructor
31
     * @param $httpClient
32
     *
33
     * @return \Vinelab\UrlShortener\Drivers\Vinelab\UrlShortener\Contracts\DriverInterface
34
     */
35
    public static function make($name, $parameters, $httpClient = null)
36
    {
37
        if (!$name) {
38
            throw new MissingConfigurationException('The config file is missing the (Default Driver Name)');
39
        }
40
41
        // prepare the full driver class name
42
        $driver_class = self::DRIVERS_NAMESPACE.ucwords($name);
43
44
        if (!class_exists($driver_class)) {
45
            throw new UnsupportedDriverException("The driver ($name) is not supported.");
46
        }
47
48
        // initialize the driver object and pass the parameters to it's constructor
49
        $driver_object = new $driver_class($parameters, $httpClient);
50
51
        return $driver_object;
52
    }
53
}
54