SocialFactory::getAdapterClassname()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
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
}