SwivelFeature   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 74
ccs 12
cts 17
cp 0.7059
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A formatRow() 0 6 2
A getContainer() 0 3 1
A getMapData() 0 19 3
1
<?php
2
3
namespace LaravelSwivel\Entity;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\Cache;
8
use Illuminate\Support\Facades\Log;
9
10
/**
11
 * Class SwivelFeature
12
 *
13
 * @package LaravelSwivel\Entity
14
 * @property integer $id
15
 * @property string $slug
16
 * @property string $buckets
17
 * @property \DateTime $created_at
18
 * @property \DateTime $updated_at
19
 */
20
class SwivelFeature extends Model implements SwivelModelInterface
21
{
22
    /**
23
     * The table associated with the model.
24
     *
25
     * @var string
26
     */
27
    protected $table = 'swivel_features';
28
29
    const SWIVEL_MAP_CACHE_KEY = 'swivel_features_map';
30
    const DELIMITER = ',';
31
32
    /**
33
     * The attributes that are mass assignable.
34
     *
35
     * @var array
36
     */
37
    protected $fillable = ['slug', 'buckets'];
38
39
    /**
40
     * Mockable function to access the container
41
     *
42
     * @return Container
43
     */
44 3
    protected function getContainer()
45
    {
46 3
        return Container::getInstance();
47
    }
48
49
    /**
50
     * Return an array of map data in the format that Swivel expects
51
     *
52
     * @return array Array containing the mapping between the features and buckets
53
     * <pre>
54
     * return [
55
     *  'TopupProviders' => [1,2,3,4,5,6,7,8,9,10],
56
     *  'TopupProviders.Multiple' => [1,2,3,4,5],
57
     * ]
58
     * </pre>
59
     */
60 3
    public function getMapData()
61
    {
62
        // load configuration settings
63 3
        $config = (array)$this->getContainer()->make('config')->get('swivel');
64
65 3
        if (empty($config)) {
66
            Log::warning('Swivel config not found.');
67
            $config['cache_key'] = '';
68
            $config['cache_duration'] = 0;
69
        }
70
71
        return Cache::remember($config['cache_key'], $config['cache_duration'], function () {
72 3
            $features = self::all();
73 3
            if ($features->count() === 0) {
74
                return [];
75
            } else {
76 3
                return call_user_func_array('array_merge', array_map([$this, 'formatRow'], $features->toArray()));
77
            }
78
            return $features;
0 ignored issues
show
Unused Code introduced by
return $features is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
79 3
        });
80
    }
81
82
    /**
83
     * Format data from database to the data swivel expects
84
     *
85
     * @param array $feature Feature record with behavior and buckets
86
     * @return array
87
     */
88 3
    protected function formatRow(array $feature)
89
    {
90 3
        if (!empty($feature['id'])) {
91 3
            return [$feature['slug'] => explode(static::DELIMITER, $feature['buckets'])];
92
        }
93
        return [];
94
    }
95
}
96