Passed
Pull Request — master (#18)
by
unknown
21:51 queued 10:53
created

MatomoController::getDateOrPeriodFromRequest()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 18
nop 2
dl 0
loc 38
rs 8.6186
c 0
b 0
f 0
1
<?php
2
3
namespace Chuckbe\Chuckcms\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Foundation\Bus\DispatchesJobs;
7
use Illuminate\Routing\Controller as BaseController;
8
use Illuminate\Foundation\Validation\ValidatesRequests;
9
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
10
use Chuckbe\Chuckcms\Models\Site;
11
use Chuckbe\Chuckcms\Chuck\SiteRepository;
12
use Chuckbe\Chuckcms\Models\User;
13
use ChuckSite;
0 ignored issues
show
Bug introduced by
The type ChuckSite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Chuckbe\Chuckcms\Chuck\Matomo\QueryFactory;
15
use Carbon\Carbon;
16
17
class MatomoController extends BaseController
18
{
19
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
20
21
    private $site;
22
    private $siteRepository;
23
    private $user;
24
    private $siteId;
25
    private $authToken;
26
    private $matomoUrl;
27
28
    /**
29
     * Create a new controller instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct(Site $site, SiteRepository $siteRepository, User $user)
34
    {
35
        $this->site = $site;
36
        $this->siteId = ChuckSite::getSetting('integrations.matomo-site-id');
37
        $this->authToken = ChuckSite::getSetting('integrations.matomo-auth-key');
38
        $this->siteRepository = $siteRepository;
39
        $this->user = $user;
40
        $this->matomoUrl = ChuckSite::getSetting('integrations.matomo-site-url');
41
    }
42
43
    public function reportingApi(Request $request)
44
    {
45
        $query_factory = QueryFactory::create($this->matomoUrl);
46
        $query_factory
47
            ->set('idSite', $this->siteId)
48
            ->set('token_auth', $this->authToken);
49
50
        $date = $this->getDateOrPeriodFromRequest($request);
51
        $period = $this->getDateOrPeriodFromRequest($request, true);
52
53
        $lastVisitsDetails = $query_factory->getQuery('Live.getLastVisitsDetails')
54
            ->setParameter('date', $date)
55
            ->setParameter('period', $period)
56
            ->setParameter('filter_limit', -1)
57
            ->execute()
58
            ->getResponse();
59
60
        $view = view('chuckcms::backend.dashboard.blocks.logs')->render();
61
62
        return response()->json(
63
            [
64
                'lastVisitsDetails' => $lastVisitsDetails,
65
                'htmlData' => $view
66
            ]
67
        );
68
    }
69
70
    public function visitorSummary(Request $request)
71
    {
72
        $query_factory = QueryFactory::create($this->matomoUrl);
73
        $query_factory
74
            ->set('idSite', $this->siteId)
75
            ->set('token_auth', $this->authToken);
76
77
        $visitorProfile = $query_factory->getQuery('Live.getVisitorProfile')
78
            ->setParameter('visitorId', $request->all()['visitorid'])
79
            ->execute()
80
            ->getResponse();
81
82
        $view = view('chuckcms::backend.dashboard.blocks.visitor_modal')->render();
83
84
        return response()->json(
85
            [
86
                'visitorProfile' => $visitorProfile,
87
                'visitorModal' => $view
88
            ]
89
        );
90
    }
91
92
    public function getLiveCounter()
93
    {
94
        $query_factory = QueryFactory::create($this->matomoUrl);
95
        $query_factory
96
            ->set('idSite', $this->siteId)
97
            ->set('token_auth', $this->authToken);
98
99
        $liveCounter = $query_factory->getQuery('Live.getCounters')
100
            ->setParameter('lastMinutes', 3)
101
            ->execute()
102
            ->getResponse();
103
104
        return response()->json(
105
            [
106
                'liveCounter' => $liveCounter
107
            ]
108
        );
109
    }
110
111
    public function getVisitsData(Request $request)
112
    {
113
        $date = $this->getDateOrPeriodFromRequest($request);
114
        $period = $this->getDateOrPeriodFromRequest($request, true);
115
116
        $imgDate = '';
117
118
        switch ($period) {
119
            case 'range':
120
                $imgDate = $date;
121
                break;
122
123
            case 'week':
124
            case 'month':
125
                $value = $request->all()["value"];
126
                $range = array(
127
                    'start' => $value["y2"] . '-' . $value["m2"] . '-' . $value["d2"],
128
                    'end'   => $value["y1"] . '-' . $value["m1"] . '-' . $value["d1"]
129
                );
130
                $imgDate = $range['start'] . ',' . $range['end'];
131
                break;
132
133
            case 'day':
134
                if ($date == 'today') {
135
                    $imgDate = date('Y-m-d') . "," . date('Y-m-d', strtotime(date('Y-m-d') . " +2 day"));
136
                }
137
                if ($date == 'yesterday') {
138
                    $imgDate = date('Y-m-d') . "," . date('Y-m-d', strtotime(date('Y-m-d') . " +2 day"));
139
                }
140
                break;
141
        }
142
143
        $matomoUrl = $this->matomoUrl;
144
        $query_factory = QueryFactory::create($matomoUrl);
145
146
        $query_factory
147
            ->set('idSite', $this->siteId)
148
            ->set('token_auth', $this->authToken);
149
150
        $data = $query_factory->getQuery('API.get ')
151
            ->setParameter('date', $date)
152
            ->setParameter('period', $period)
153
            ->execute()
154
            ->getResponse();
155
156
        return response()->json(
157
            [
158
                'visitimg' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&action=get&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&segment=&showtitle=1&random=6179&columns=nb_visits%2Cnb_uniq_visitors&token_auth=' . $this->authToken,
159
                'avgvisitimg' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&action=get&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&showtitle=1&random=6179&columns=avg_time_on_site&token_auth=' . $this->authToken,
160
                'bouncerateimg' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&columns=bounce_rate&token_auth=' . $this->authToken,
161
                'actions_per_visit_img' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&action=get&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&columns=nb_actions_per_visit&token_auth=' . $this->authToken,
162
                'pageviewimg' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&action=get&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&columns=nb_pageviews%2Cnb_uniq_pageviews&token_auth=' . $this->authToken,
163
                'searchesandkeywordsimg' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&action=get&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&columns=nb_searches%2Cnb_keywords&token_auth=' . $this->authToken,
164
                'downloadsimg' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&action=get&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&columns=nb_downloads%2Cnb_uniq_downloads&token_auth=' . $this->authToken,
165
                'outlinksimg' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&action=get&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&columns=nb_outlinks%2Cnb_uniq_outlinks&token_auth=' . $this->authToken,
166
                'maxactionsimg' => $matomoUrl . '/index.php?forceView=1&viewDataTable=sparkline&module=API&action=get&idSite=' . $this->siteId . '&period=' . $period . '&date=' . $imgDate . '&columns=max_actions&token_auth=' . $this->authToken,
167
                'data' => $data
168
            ]
169
        );
170
    }
171
172
    public function getReferrers(Request $request)
173
    {
174
        $query_factory = QueryFactory::create($this->matomoUrl);
175
176
        $query_factory
177
            ->set('idSite', $this->siteId)
178
            ->set('token_auth', $this->authToken);
179
180
        $date = $this->getDateOrPeriodFromRequest($request);
181
        $period = $this->getDateOrPeriodFromRequest($request, true);
182
183
        $referrers = $query_factory->getQuery('Referrers.getAll')
184
            ->setParameter('date', $date)
185
            ->setParameter('period', $period)
186
            ->execute()
187
            ->getResponse();
188
189
        return response()->json(
190
            [
191
                'data' => $referrers
192
            ]
193
        );
194
    }
195
196
    private function getDateOrPeriodFromRequest(Request $request, $periodCheck = false)
197
    {
198
        $data = $request->all()['value'];
199
        $date = strtolower($data["range"]);
200
        $period = 'day';
201
202
        if (!isset($data["y2"], $data["m2"], $data["d2"])) {
203
            return $periodCheck ? $period : $date;
204
        }
205
206
        $range = array(
207
            'start' => $data["y2"] . '-' . $data["m2"] . '-' . $data["d2"],
208
            'end'   => $data["y1"] . '-' . $data["m1"] . '-' . $data["d1"]
209
        );
210
211
        $now = Carbon::now();
212
        $start = Carbon::createFromFormat('Y-m-d', $range['start']);
213
        $end = Carbon::createFromFormat('Y-m-d', $range['end']);
214
215
        $difference = $now->diffInDays($end); // difference in days between end date and now
216
        $diffStartToEnd = $start->diffInDays($end); //difference in days between start date and end date
217
218
        if ($diffStartToEnd == 6) {
219
            $period = 'week';
220
            $date = 'last7';
221
        }
222
223
        if ($diffStartToEnd == 29) {
224
            $period = 'month';
225
            $date = 'last30';
226
        }
227
228
        if ($difference > 0) {
229
            $period = 'range';
230
            $date = $range['start'] . ',' . $range['end'];
231
        }
232
233
        return $periodCheck ? $period : $date;
234
    }
235
}
236