Completed
Pull Request — master (#2)
by
unknown
23:43
created

ActivityLogController::getFieldDescriptors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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 Exception;
15
use FOS\RestBundle\Controller\Annotations\Get;
16
use FOS\RestBundle\Controller\FOSRestController;
17
use FOS\RestBundle\Routing\ClassResourceInterface;
18
use Sulu\Bundle\ActivityLogBundle\Compatibility\FieldDescriptor;
19
use Sulu\Component\ActivityLog\ActivityLogger;
20
use Sulu\Component\ActivityLog\ActivityLoggerInterface;
21
use Sulu\Component\Rest\Exception\EntityNotFoundException;
22
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
23
use Sulu\Component\Rest\ListBuilder\ListRestHelper;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
27
28
class ActivityLogController extends FOSRestController implements ClassResourceInterface
29
{
30
31
    const EXPORT_COLUMN_DELIMITER = ';';
32
    const EXPORT_FILENAME = 'acitivity-log-export';
33
34
    /**
35
     * Returns all fields that can be used by list.
36
     *
37
     * @Get("activity-log/fields")
38
     *
39
     * @param Request $request
40
     *
41
     * @return mixed
42
     */
43
    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...
44
    {
45
        // default contacts list
46
        return $this->handleView(
47
            $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...
48
                array_values(
49
                    $this->getFieldDescriptors()
50
                ),
51
                200
52
            )
53
        );
54
    }
55
56
    /**
57
     * Create field-descriptor array.
58
     *
59
     * @return FieldDescriptor[]
60
     */
61
    private function getFieldDescriptors()
62
    {
63
        return [
64
            'uuid' => new FieldDescriptor('id', 'public.id', true, false),
65
            'data' => new FieldDescriptor('data', 'public.data', false, true),
66
        ];
67
    }
68
69
    /**
70
     * Shows all activity-log-items
71
     *
72
     * @param Request $request
73
     *
74
     * @Get("activity-log")
75
     *
76
     * @return \Symfony\Component\HttpFoundation\Response
77
     */
78
    public function cgetAction(Request $request)
79
    {
80
        $list = $this->getActivityLogs($request);
81
82
        $view = $this->view($list, 200);
83
84
        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...
85
    }
86
87
    /**
88
     * returns datagrid list of activity-log for export
89
     *
90
     * @param Request $request
91
     *
92
     * @Get("activity-log/export")
93
     *
94
     * @return Response
95
     */
96
    public function getActivityLogExportAction(Request $request)
97
    {
98
        try {
99
            $list = $this->getActivityLogs($request);
100
101
            return $this->generateCsvResponse($this->listToCsv($list, self::EXPORT_COLUMN_DELIMITER));
102
        } catch (Exception $e) {
103
            $view = $this->view(array($e->getMessage()), 400);
104
        }
105
106
        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...
107
    }
108
109
    /**
110
     * returns view of files
111
     *
112
     * @param Request $request
113
     *
114
     * @throws EntityNotFoundException
115
     *
116
     * @return ListRepresentation
117
     */
118
    private function getActivityLogs(Request $request)
119
    {
120
        $restHelper = new ListRestHelper($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Symfony\Component\HttpFoundation\Request>, but the function expects a object<Symfony\Component...oundation\RequestStack>.

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...
121
122
        /** @var ActivityLogger $activityLogger */
123
        $activityLogger = $this->get('sulu_activity_log.activity_logger');
124
125
        $page = (int)$restHelper->getPage();
126
        $limit = (int)$restHelper->getLimit();
127
128
        $list = $activityLogger->findAll($page, $limit);
129
        $list = array_values($list);
130
131
        $list = new ListRepresentation(
132
            $list,
133
            'activity-log-items',
134
            'get_activity_logs',
135
            $request->query->all(),
136
            $page,
137
            $limit,
138
            count($list)
139
        );
140
141
        return $list;
142
    }
143
144
    /**
145
     * @param ListRepresentation $list
146
     * @param string $delimiter
147
     *
148
     * @return string
149
     */
150
    private function listToCsv($list, $delimiter)
151
    {
152
        $data = $list->getInline()->getResources();
153
        $csv = '';
154
        $headers = array_keys(reset($data));
155
        foreach ($headers as $header) {
156
            $csv .= $header . $delimiter;
157
        }
158
        $csv = rtrim($csv, $delimiter) . PHP_EOL;
159
160
        // iterate over data array
161
        foreach ($data as $dataline) {
162
            $csv .= $this->addLine($dataline, $delimiter);
163
        }
164
165
        return $csv;
166
    }
167
168
    /**
169
     * @param array $dataline
170
     * @param string $delimiter
171
     *
172
     * @return string
173
     */
174
    private function addLine($dataline, $delimiter)
175
    {
176
        $csvLine = '';
177
        foreach ($dataline as $datafield) {
178
            if ($datafield instanceof DateTime) {
0 ignored issues
show
Bug introduced by
The class Sulu\Bundle\ActivityLogBundle\Controller\DateTime does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
179
                $csvLine .= $datafield->format(DateTime::ISO8601);
180
            } elseif (is_scalar($datafield)) {
181
                $csvLine .= $datafield;
182
            }
183
            $csvLine .= $delimiter;
184
        }
185
        $csvLine = rtrim($csvLine, $delimiter) . PHP_EOL;
186
187
        return $csvLine;
188
    }
189
190
    /**
191
     * @param string $csv
192
     *
193
     * @return Response
194
     */
195
    private function generateCsvResponse($csv)
196
    {
197
        $response = new Response();
198
        $response->setContent($csv);
199
200
        $name = self::EXPORT_FILENAME . '-' . date('Ymd') . '.csv';
201
        $disponent = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $name);
202
        $response->headers->set('Content-Disposition', $disponent);
203
        $response->headers->set('Content-Type', 'text/csv');
204
205
        return $response;
206
    }
207
}
208