1 | <?php |
||
11 | class RecentlyViewedBehavior extends Behavior |
||
12 | { |
||
13 | /** |
||
14 | * @param integer the limit to the number of items in the recently viewed list. |
||
15 | */ |
||
16 | public $limit = 8; // 0 = no limit. |
||
17 | |||
18 | /** |
||
19 | * Adds an item id to a 'recently viewed items' session object. |
||
20 | * @var string the modelClass of the item to store |
||
21 | * @param integer the id of the item to store |
||
22 | */ |
||
23 | |||
24 | 2 | public function setRecentlyViewed($modelClass, $id) |
|
25 | { |
||
26 | // Create the session index |
||
27 | 2 | $index = $modelClass . '_recently_viewed'; |
|
28 | // Check if the session index exists |
||
29 | 2 | if (!(Yii::$app->session->has($index))) { |
|
30 | 2 | $recentlyViewed = []; |
|
|
|||
31 | } |
||
32 | 2 | $recentlyViewed = Yii::$app->session->get($index, []); |
|
33 | // Remove the id if it is already in the list |
||
34 | 2 | if (($key = array_search($id, $recentlyViewed)) !== false) { |
|
35 | 1 | unset($recentlyViewed[$key]); |
|
36 | } |
||
37 | // If a limit is set, and the list is at (or over) the limit, remove oldest item(s) |
||
38 | 2 | if ($this->limit > 0 && count($recentlyViewed) >= $this->limit) { |
|
39 | 1 | $count = count($recentlyViewed) - $this->limit; |
|
40 | 1 | $recentlyViewed = array_slice($recentlyViewed, $count); |
|
41 | } |
||
42 | // Add the current item id to the end of the array |
||
43 | 2 | array_push($recentlyViewed, $id); |
|
44 | // Update the session |
||
45 | 2 | Yii::$app->session->set($index, $recentlyViewed); |
|
46 | 2 | } |
|
47 | |||
48 | /** |
||
49 | * Retrieves model records from a 'recently viewed items' session object. |
||
50 | * @param string $modelClass the modelClass of the items to retrieve |
||
51 | * @return array|null|static[] |
||
52 | */ |
||
53 | 2 | public function getRecentlyViewed($modelClass) |
|
54 | { |
||
55 | // Create the session index |
||
56 | 2 | $index = $modelClass . '_recently_viewed'; |
|
57 | 2 | $models = array(); |
|
58 | // Check if the session index exists |
||
59 | 2 | if (isset(Yii::$app->session[$index])) { |
|
60 | /* @var $recentlyViewed \zacksleo\yii2\behaviors\Dictionary */ |
||
61 | 2 | $recentlyViewed = Yii::$app->session[$index]; |
|
62 | // Check if a limit is set, and if the list is at (or over) the limit |
||
63 | 2 | if ($this->limit > 0 && count($recentlyViewed) >= $this->limit) { |
|
64 | 1 | $count = count($recentlyViewed) - $this->limit; |
|
65 | // Remove the oldest item(s) (always an index of 0 after each removal) |
||
66 | 1 | $recentlyViewed = array_slice($recentlyViewed, $count); |
|
67 | } |
||
68 | // Reverse the array so the most recently added item is first |
||
69 | 2 | $recentlyViewed = array_reverse($recentlyViewed); |
|
70 | // Create a comma separated list for the db order property |
||
71 | 2 | $commaSeparated = implode(',', $recentlyViewed); |
|
72 | // Find all of the models with the array of ids recently viewed |
||
73 | // and order the results in the same order as the array |
||
74 | 2 | $models = $modelClass::find()->where(['id' => $recentlyViewed])->orderBy([new yii\db\Expression("FIELD (id, $commaSeparated)")])->all(); |
|
75 | } |
||
76 | 2 | return $models; |
|
77 | } |
||
78 | |||
79 | 1 | public function clearRecentlyViewed($modelClass) |
|
91 | } |
||
92 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.