Activity::activity()   B
last analyzed

Complexity

Conditions 9
Paths 130

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 35.321

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 53
rs 7.8055
ccs 10
cts 32
cp 0.3125
cc 9
nc 130
nop 2
crap 35.321

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace mQueue\View\Helper;
4
5
use mQueue\Model\MovieMapper;
6
use mQueue\Model\UserMapper;
7
use Zend_Date;
8
use Zend_Paginator;
9
use Zend_View_Helper_Abstract;
10
11
class Activity extends Zend_View_Helper_Abstract
12
{
13
    /**
14
     * Returns an HTML table of activities
15
     *
16
     * @param Zend_Paginator $activity
17
     * @param array $hiddenColumns optionally hidden columns
18
     *
19
     * @return string
20
     */
21 3
    public function activity(Zend_Paginator $activity, $hiddenColumns = [])
22
    {
23 3
        $columns = ['date', 'user', 'status', 'movie'];
24 3
        $columns = array_diff($columns, $hiddenColumns);
25
26 3
        $result = '<div class="activity">';
27
28 3
        $cacheUser = [];
29 3
        $cacheMovie = [];
30 3
        foreach ($activity as $status) {
31
            if (!array_key_exists($status->idUser, $cacheUser)) {
32
                $cacheUser[$status->idUser] = UserMapper::find($status->idUser);
33
            }
34
            $user = $cacheUser[$status->idUser];
35
36
            if (!array_key_exists($status->idMovie, $cacheMovie)) {
37
                $cacheMovie[$status->idMovie] = MovieMapper::find($status->idMovie);
38
            }
39
            $movie = $cacheMovie[$status->idMovie];
40
41
            $result .= '<div class="row">';
42
43
            if (in_array('user', $columns)) {
44
                $result .= '<div class="user"><a href="' . $this->view->url([
45
                        'controller' => 'user',
46
                        'action' => 'view',
47
                        'id' => $user->id,
48
                    ], 'singleid', true) . '">' . $this->view->gravatar($user, 'medium') . ' ' /* . $this->view->escape($user->nickname) */ . '</a></div>';
49
            }
50
51
            $result .= '<div class="others">';
52
53
            if (in_array('movie', $columns)) {
54
                $result .= '<div class="movie">' . $this->view->movie($movie) . '</div>';
55
            }
56
            if (in_array('status', $columns)) {
57
                $result .= '<span class="rating">' . $this->view->statusLinks($status) . '</span> ';
58
            }
59
60
            if (in_array('date', $columns)) {
61
                $result .= '<time class="dateUpdate timestamp" datetime="' . $status->getDateUpdate()->get(Zend_Date::ISO_8601) . '" title="' . $status->getDateUpdate()->get(Zend_Date::ISO_8601) . '">' . $status->dateUpdate . '</time>';
62
            }
63
64
            $result .= '</div>';
65
66
            $result .= '</div>';
67
        }
68
69 3
        if ($activity->getTotalItemCount() == 0) {
70 3
            $result .= '<p>' . $this->view->translate('There is no activity to show.') . '<p>';
71
        }
72
73 3
        return $result . '</div>';
74
    }
75
}
76