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; |
|
|
|
|
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
|
|
|
|
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
orexit
statements that have been added for debug purposes.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.