Completed
Push — master ( f9f928...a80184 )
by Wachter
04:01
created

ActivityLogController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 53.49%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 110
ccs 23
cts 43
cp 0.5349
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldsAction() 0 12 1
A cgetAction() 0 8 1
A getFieldDescriptors() 0 10 1
B getActivityLogs() 0 39 1
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\ActivityLogBundle\Controller;
13
14
use FOS\RestBundle\Controller\Annotations\Get;
15
use FOS\RestBundle\Controller\FOSRestController;
16
use FOS\RestBundle\Routing\ClassResourceInterface;
17
use Sulu\Component\ActivityLog\ActivityLogger;
18
use Sulu\Component\Rest\Exception\EntityNotFoundException;
19
use Sulu\Component\Rest\ListBuilder\FieldDescriptor;
20
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
21
use Symfony\Component\HttpFoundation\Request;
22
23
/**
24
 * Makes activity logs available through a REST API.
25
 */
26
class ActivityLogController extends FOSRestController implements ClassResourceInterface
27
{
28
    const EXPORT_COLUMN_DELIMITER = ';';
29
    const EXPORT_FILENAME = 'activity-log-export';
30
31
    /**
32
     * Returns all fields that can be used by list.
33
     *
34
     * @Get("activity-log/fields")
35
     *
36
     * @param Request $request
37
     *
38
     * @return mixed
39
     */
40
    public function getFieldsAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        // default contacts list
43
        return $this->handleView(
44
            $this->view(
0 ignored issues
show
Documentation introduced by
$this->view(array_values...eldDescriptors()), 200) is of type this<Sulu\Bundle\Activit...\ActivityLogController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
                array_values(
46
                    $this->getFieldDescriptors()
47
                ),
48
                200
49
            )
50
        );
51
    }
52
53
    /**
54
     * Shows all activity-log-items.
55
     *
56
     * @param Request $request
57
     *
58
     * @Get("activity-log")
59
     *
60
     * @return \Symfony\Component\HttpFoundation\Response
61
     */
62 5
    public function cgetAction(Request $request)
63
    {
64 5
        $list = $this->getActivityLogs($request);
65
66 5
        $view = $this->view($list, 200);
67
68 5
        return $this->handleView($view);
0 ignored issues
show
Documentation introduced by
$view is of type this<Sulu\Bundle\Activit...\ActivityLogController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
    }
70
71
    /**
72
     * Create field-descriptor array.
73
     *
74
     * @return FieldDescriptor[]
75
     */
76
    protected function getFieldDescriptors()
77
    {
78
        return [
79
            'uuid' => new FieldDescriptor('uuid', 'public.id', true, false),
80
            'type' => new FieldDescriptor('type', 'sulu_activity_log.type', false, true),
81
            'title' => new FieldDescriptor('title', 'public.title', false, true),
82
            'message' => new FieldDescriptor('message', 'sulu_activity_log.message', false, true),
83
            'created' => new FieldDescriptor('created', 'public.created', false, true, 'datetime'),
84
        ];
85
    }
86
87
    /**
88
     * returns view of files.
89
     *
90
     * @param Request $request
91
     *
92
     * @throws EntityNotFoundException
93
     *
94
     * @return ListRepresentation
95
     */
96 5
    protected function getActivityLogs(Request $request)
97
    {
98 5
        $restHelper = $this->get('sulu_core.list_rest_helper');
99
100
        /** @var ActivityLogger $activityLogger */
101 5
        $activityLogger = $this->get('sulu_activity_log.activity_logger');
102
103 5
        $page = (int) $restHelper->getPage();
104 5
        $limit = (int) $restHelper->getLimit();
105 5
        $sortColumn = $restHelper->getSortColumn();
106 5
        $sortOrder = $restHelper->getSortOrder();
107 5
        $searchPattern = $restHelper->getSearchPattern();
108 5
        $searchFields = $restHelper->getSearchFields();
109
110 5
        $list = $activityLogger->findAllWithSearch(
111
            $searchPattern,
112
            $searchFields,
113
            $page,
114
            $limit,
115
            $sortColumn,
116 5
            $sortOrder
117
        );
118
119 5
        $total = $activityLogger->getCountForAllWithSearch($searchPattern, $searchFields);
120
121 5
        $list = array_values($list);
122
123 5
        $list = new ListRepresentation(
124
            $list,
125 5
            'activity-log-items',
126 5
            'get_activity_logs',
127 5
            $request->query->all(),
128
            $page,
129
            $limit,
130 5
            $total
131
        );
132
133 5
        return $list;
134
    }
135
}
136