RecentlyViewedBehavior   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 77
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setRecentlyViewed() 0 19 4
A getRecentlyViewed() 0 25 4
A clearRecentlyViewed() 0 12 2
1
<?php
2
3
namespace zacksleo\yii2\behaviors;
4
5
use yii;
6
use yii\base\Behavior;
7
8
/**
9
 * ERecentlyViewedBehavior is a behavior for managing recently viewed model items.
10
 */
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 2
        $recentlyViewed = Yii::$app->session->get($index, []);
29
        // Remove the id if it is already in the list
30 2
        if (($key = array_search($id, $recentlyViewed)) !== false) {
31 1
            unset($recentlyViewed[$key]);
32
        }
33
        // If a limit is set, and the list is at (or over) the limit, remove oldest item(s)
34 2
        if ($this->limit > 0 && count($recentlyViewed) >= $this->limit) {
35 1
            $count = count($recentlyViewed) - $this->limit;
36 1
            $recentlyViewed = array_slice($recentlyViewed, $count);
37
        }
38
        // Add the current item id to the end of the array
39 2
        array_push($recentlyViewed, $id);
40
        // Update the session
41 2
        Yii::$app->session->set($index, $recentlyViewed);
42 2
    }
43
44
    /**
45
     * Retrieves model records from a 'recently viewed items' session object.
46
     * @param string $modelClass the modelClass of the items to retrieve
47
     * @return array|null|static[]
48
     */
49 2
    public function getRecentlyViewed($modelClass)
50
    {
51
        // Create the session index
52 2
        $index = $modelClass . '_recently_viewed';
53 2
        $models = array();
54
        // Check if the session index exists
55 2
        if (isset(Yii::$app->session[$index])) {
56
            /* @var $recentlyViewed \zacksleo\yii2\behaviors\Dictionary */
57 2
            $recentlyViewed = Yii::$app->session[$index];
58
            // Check if a limit is set, and if the list is at (or over) the limit
59 2
            if ($this->limit > 0 && count($recentlyViewed) >= $this->limit) {
60 1
                $count = count($recentlyViewed) - $this->limit;
61
                // Remove the oldest item(s) (always an index of 0 after each removal)
62 1
                $recentlyViewed = array_slice($recentlyViewed, $count);
63
            }
64
            // Reverse the array so the most recently added item is first
65 2
            $recentlyViewed = array_reverse($recentlyViewed);
66
            // Create a comma separated list for the db order property
67 2
            $commaSeparated = implode(',', $recentlyViewed);
68
            // Find all of the models with the array of ids recently viewed
69
            // and order the results in the same order as the array
70 2
            $models = $modelClass::find()->where(['id' => $recentlyViewed])->orderBy([new yii\db\Expression("FIELD (id, $commaSeparated)")])->all();
71
        }
72 2
        return $models;
73
    }
74
75 1
    public function clearRecentlyViewed($modelClass)
76
    {
77
        // Create the session index
78 1
        $index = $modelClass . '_recently_viewed';
79
        // Check if the session index exists
80 1
        if (isset(Yii::$app->session[$index])) {
81 1
            unset(Yii::$app->session[$index]);
82
        }
83 1
        echo json_encode([
84 1
            'success' => true
85
        ]);
86 1
    }
87
}
88