ProviderFactory::create()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 9
nc 6
nop 1
1
<?php
2
3
namespace Vinelab\Cdn;
4
5
use Illuminate\Support\Facades\App;
6
use Vinelab\Cdn\Contracts\ProviderFactoryInterface;
7
use Vinelab\Cdn\Exceptions\MissingConfigurationException;
8
use Vinelab\Cdn\Exceptions\UnsupportedProviderException;
9
10
/**
11
 * Class ProviderFactory
12
 * This class is responsible of creating objects from the default
13
 * provider found in the config file.
14
 *
15
 * @category Factory
16
 *
17
 * @author  Mahmoud Zalt <[email protected]>
18
 */
19
class ProviderFactory implements ProviderFactoryInterface
20
{
21
    const DRIVERS_NAMESPACE = 'Vinelab\\Cdn\\Providers\\';
22
23
    /**
24
     * Create and return an instance of the corresponding
25
     * Provider concrete according to the configuration.
26
     *
27
     * @param array $configurations
28
     *
29
     * @return mixed
30
     *
31
     * @throws \Vinelab\Cdn\UnsupportedDriverException
32
     */
33
    public function create($configurations = array())
34
    {
35
        // get the default provider name
36
        $provider = isset($configurations['default']) ? $configurations['default'] : null;
37
38
        if (!$provider) {
39
            throw new MissingConfigurationException('Missing Configurations: Default Provider');
40
        }
41
42
        // prepare the full driver class name
43
        $driver_class = self::DRIVERS_NAMESPACE.ucwords($provider).'Provider';
44
45
        if (!class_exists($driver_class)) {
46
            throw new UnsupportedProviderException("CDN provider ($provider) is not supported");
47
        }
48
49
        // initialize the driver object and initialize it with the configurations
50
        $driver_object = App::make($driver_class)->init($configurations);
51
52
        return $driver_object;
53
    }
54
}
55