Test Failed
Pull Request — master (#22)
by
unknown
02:23
created

Ratings::modifyRating()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
namespace MovingImage\Client\VMPro\Services;
4
5
use MovingImage\Client\VMPro\Entity\Video;
6
use MovingImage\Client\VMPro\Entity\VideoRequestParameters;
7
use MovingImage\Client\VMPro\Interfaces\ApiClientInterface;
8
9
/**
10
 * Class Ratings.
11
 *
12
 * @author Robert Szeker <[email protected]>
13
 */
14
class Ratings
15
{
16
    /** @var ApiClientInterface */
17
    private $client;
18
19
    /** @var Video [] */
20
    private $videos = [];
21
22
    /** @var int */
23
    private $vmId;
24
25
    /** @var string */
26
    private $metadataFieldAverage;
27
28
    /** @var string */
29
    private $metadataFieldCount;
30
31
    public function __construct(
32
        ApiClientInterface $client,
33
        $vmId,
34
        $metadataFieldAverage,
35
        $metadataFieldCount
36
    ) {
37
        $this->client = $client;
38
        $this->metadataFieldAverage = $metadataFieldAverage;
39
        $this->metadataFieldCount = $metadataFieldCount;
40
        $this->vmId = $vmId;
41
    }
42
43
    /**
44
     * Increases the count of all ratings by one and calculates a new average rating value.
45
     *
46
     * @param string $videoId
47
     * @param int    $rating
48
     */
49
    public function addRating($videoId, $rating)
50
    {
51
        $customMetaData = $this->getVideo($videoId)->getCustomMetadata();
52
53
        $average = $this->getRatingAverage($videoId);
54
        $count = $this->getRatingCount($videoId);
55
        $newCount = $count + 1;
56
57
        $customMetaData[$this->metadataFieldCount] = $newCount;
58
        $customMetaData[$this->metadataFieldAverage] = (($average * $count) + $rating) / $newCount;
59
60
        $this->storeCustomMetaData($customMetaData, $videoId);
61
    }
62
63
    /**
64
     * Modifies the average rating value. Count of all ratings stays the same (will not be increased).
65
     * The use case of this function is, when someone wants to change its rating (video was already rated by that person).
66
     *
67
     * @param string $videoId
68
     * @param int    $rating
69
     * @param int    $oldRating
70
     */
71
    public function modifyRating($videoId, $rating, $oldRating)
72
    {
73
        $customMetaData = $this->getVideo($videoId)->getCustomMetadata();
74
75
        $average = $this->getRatingAverage($videoId);
76
        $count = $this->getRatingCount($videoId);
77
78
        $customMetaData[$this->metadataFieldAverage] = (($average * $count) - $oldRating + $rating) / $count;
79
80
        $this->storeCustomMetaData($customMetaData, $videoId);
81
    }
82
83
    /**
84
     * Returns the average rating value from the custom meta data fields from a given video.
85
     *
86
     * @param string $videoId
87
     *
88
     * @return int
89
     */
90 View Code Duplication
    private function getRatingAverage($videoId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $customMetaData = $this->getVideo($videoId)->getCustomMetadata();
93
94
        return array_key_exists($this->metadataFieldAverage, $customMetaData)
95
            ? (float) $customMetaData[$this->metadataFieldAverage]
96
            : 0;
97
    }
98
99
    /**
100
     * Returns the count of all ratings from the custom meta data fields from a given video.
101
     *
102
     * @param string $videoId
103
     *
104
     * @return int
105
     */
106 View Code Duplication
    private function getRatingCount($videoId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $customMetaData = $this->getVideo($videoId)->getCustomMetadata();
109
110
        return array_key_exists($this->metadataFieldCount, $customMetaData)
111
            ? (float) $customMetaData[$this->metadataFieldCount]
112
            : 0;
113
    }
114
115
    /**
116
     * Stores the custom meta data fields with the api client.
117
     *
118
     * @param array  $customMetaData
119
     * @param string $videoId
120
     */
121
    private function storeCustomMetaData($customMetaData, $videoId)
122
    {
123
        $this->client->setCustomMetaData($this->vmId, $videoId, $customMetaData);
124
125
        // also store custom meta data fields locally, if video is fetched again by function $this->getVideo($videoId)
126
        $video = $this->getVideo($videoId);
127
        $video->setCustomMetadata($customMetaData);
128
    }
129
130
    /**
131
     * Fetches and returns video from api client and stores it locally.
132
     * This way api client will be requested only once for every video.
133
     *
134
     * @param string $videoId
135
     *
136
     * @return Video
137
     */
138
    private function getVideo($videoId)
139
    {
140
        if (!array_key_exists($videoId, $this->videos)) {
141
            $options = new VideoRequestParameters();
142
            $options->setIncludeCustomMetadata(true);
143
            $this->videos[$videoId] = $this->client->getVideo($this->vmId, $videoId, $options);
144
        }
145
146
        return $this->videos[$videoId];
147
    }
148
}
149