PersistentDataFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A createPersistentDataHandler() 0 20 6
1
<?php
2
3
namespace Maztech\PersistentData;
4
5
use InvalidArgumentException;
6
7
class PersistentDataFactory
8
{
9
    private function __construct()
10
    {
11
        // a factory constructor should never be invoked
12
    }
13
14
    /**
15
     * PersistentData generation.
16
     *
17
     * @param PersistentDataInterface|string|null $handler
18
     *
19
     * @throws InvalidArgumentException If the persistent data handler isn't "session", "memory", or an instance of Instagram\PersistentData\PersistentDataInterface.
20
     *
21
     * @return PersistentDataInterface
22
     */
23
    public static function createPersistentDataHandler($handler)
24
    {
25
        if (!$handler) {
26
            return session_status() === PHP_SESSION_ACTIVE
27
                ? new InstagramSessionPersistentDataHandler()
28
                : new InstagramMemoryPersistentDataHandler();
29
        }
30
31
        if ($handler instanceof PersistentDataInterface) {
32
            return $handler;
33
        }
34
35
        if ('session' === $handler) {
36
            return new InstagramSessionPersistentDataHandler();
37
        }
38
        if ('memory' === $handler) {
39
            return new InstagramMemoryPersistentDataHandler();
40
        }
41
42
        throw new InvalidArgumentException('The persistent data handler must be set to "session", "memory", or be an instance of Instagram\PersistentData\PersistentDataInterface');
43
    }
44
}
45