SocialFactory::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
1
<?php
2
3
4
namespace databoxtech\multisocial;
5
6
7
use databoxtech\multisocial\adapter\SocialAdapter;
8
9
class SocialFactory
10
{
11
12
    /**
13
     * Create a new instance of social adapter
14
     *
15
     * @param string $name $name
16
     * @param array $config
17
     * @return SocialAdapter
18
     */
19 2
    public function create(string $name, array $config){
20 2
        $class = $this->getAdapterClassname($name);
21 2
        if (!class_exists($class)) {
22
            throw new \RuntimeException("Class '$class' not found");
23
        }
24 2
        return new $class($config);
25
    }
26
27
    /**
28
     * @param string $name adapter name
29
     * @return string PSR-0 classpath
30
     */
31 2
    private function getAdapterClassname(string $name){
32 2
        $name = ucfirst($name);
33
        // replace underscores with namespace marker, PSR-0 style
34 2
        $name = str_replace('_', '\\', $name);
35 2
        return '\\databoxtech\\multisocial\\adapter\\'.$name.'Adapter';
36
    }
37
}