Passed
Pull Request — master (#9)
by
unknown
17:20
created

MatomoController::submit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 1
dl 0
loc 25
rs 9.6666
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 VisualAppeal\Matomo;
0 ignored issues
show
Bug introduced by
The type VisualAppeal\Matomo 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...
15
use Matomo\ReportingApi\QueryFactory;
0 ignored issues
show
Bug introduced by
The type Matomo\ReportingApi\QueryFactory 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...
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
27
    /**
28
     * Create a new controller instance.
29
     *
30
     * @return void
31
     */
32
     public function __construct(Site $site, SiteRepository $siteRepository, User $user)
33
    {
34
        $this->site = $site;
35
        $this->siteId = ChuckSite::getSetting('integrations.matomo-site-id');
36
        $this->authToken = ChuckSite::getSetting('integrations.matomo-auth-key');
37
        $this->siteRepository = $siteRepository;
38
        $this->user = $user;
39
    }
40
41
 
42
    /**
43
     * Show the dashboard -> pages.
44
     *
45
     * @return \Illuminate\View\View
46
     */
47
    // public function index()
48
    // {
49
    //     if($this->siteId == null || $this->authToken == null){
50
    //         return view('chuckcms::backend.matomo.index', ['matomoSiteId' => $this->siteId, 'matomoAuthToken' => $this->authToken]);
51
    //     }
52
    //     dd($this->siteId);
53
    //     return view('chuckcms::backend.matomo.index');
54
    // }
55
    public function ReportingApi(Request $request)
56
    {
57
        $data = $request->all();
58
        $matomoUrl = ChuckSite::getSetting('integrations.matomo-site-url') !== null ? ChuckSite::getSetting('integrations.matomo-site-url') : config('chuckcms.analytics.matomoURL');
59
        $query_factory = QueryFactory::create($matomoUrl);
60
        $query_factory
61
            ->set('idSite', $this->siteId)
62
            ->set('token_auth', $this->authToken);
63
        
64
        $date = 'today';
65
        $period = 'day';
66
        
67
        if($data["value"]["range"] !== "Today" || $data["value"]["range"] !== "Yesterday"){        
68
            if(isset($data["value"]["y2"],$data["value"]["m2"],$data["value"]["d2"])){
69
                $now = \Carbon\Carbon::now();
70
                $startdate = \Carbon\Carbon::createFromFormat('Y-m-d', $data["value"]["y2"].'-'.$data["value"]["m2"].'-'.$data["value"]["d2"]);
71
                $enddate = \Carbon\Carbon::createFromFormat('Y-m-d',$data["value"]["y1"].'-'.$data["value"]["m1"].'-'.$data["value"]["d1"]);
72
                $checkforrange = $now->diffInDays($enddate);
73
                $diff = $startdate->diffInDays($enddate);
74
                if($checkforrange !== 0){
75
                    $period = 'range';
76
                    $date = $data["value"]["y2"].'-'.$data["value"]["m2"].'-'.$data["value"]["d2"].','.$data["value"]["y1"].'-'.$data["value"]["m1"].'-'.$data["value"]["d1"];                    
77
                }else{
78
                    if($diff == 6){
79
                        $period = 'week';
80
                        $date = 'last7';
81
                    }
82
                    if($diff == 29){
83
                        $period = 'month';
84
                        $date = 'last30';
85
                    }
86
                }
87
            }      
88
        }
89
        if($data["value"]["range"] == "Today"){
90
            $date = 'today';
91
            $period = 'day';
92
        }
93
        if($data["value"]["range"] == "Yesterday"){
94
            $date = 'yesterday';
95
            $period = 'day';
96
        }
97
        
98
        // $visitsSummary = $query_factory->getQuery('VisitsSummary.get')
99
        // ->setParameter('date', $date)
100
        // ->setParameter('period', 'day')                
101
        // ->execute()
102
        // ->getResponse();
103
104
            
105
        $lastVisitsDetails = $query_factory->getQuery('Live.getLastVisitsDetails')
106
            ->setParameter('date', $date)
107
            ->setParameter('period', $period)
108
            ->setParameter('filter_limit', -1)
109
            ->execute()
110
            ->getResponse();
111
        
112
              
113
        // $heatMaps = $query_factory->getQuery('HeatmapSessionRecording.getHeatmaps')
114
        // ->execute()
115
        // ->getResponse();
116
        
117
        // $heatMap = array();
118
        // foreach($heatMaps as $key=>$value)
119
        // {
120
            
121
        //     $heatMap[] = $query_factory->getQuery('HeatmapSessionRecording.getHeatmap')
122
        //     ->setParameter('idSiteHsr', $value->idsitehsr)
123
        //     ->execute()
124
        //     ->getResponse();
125
            
126
        // }
127
128
129
        return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...=> $lastVisitsDetails)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\View\View.
Loading history...
130
            'success'=>'success',
131
            'lastVisitsDetails' => $lastVisitsDetails,
132
        ]);
133
134
    }
135
136
    // public function visitorsummary(Request $request)
137
    // {
138
    //     $data = $request->all();
139
    //     $query_factory = QueryFactory::create(config('chuckcms.analytics.matomoURL'));
140
    //     $query_factory
141
    //         ->set('idSite', $this->siteId)
142
    //         ->set('token_auth', $this->authToken);
143
144
    //     $visitorProfile = $query_factory->getQuery('Live.getVisitorProfile')
145
    //         ->setParameter('visitorId', $data['visitorid'])
146
    //         ->execute()
147
    //         ->getResponse();
148
        
149
    //     return response()->json([
150
    //         'success'=>'success',
151
    //         'visitorProfile' => $visitorProfile
152
    //     ]);
153
    // }
154
155
    // public function Livecounter(Request $request)
156
    // {
157
    //     $data = $request->all();
158
    //     $query_factory = QueryFactory::create(config('chuckcms.analytics.matomoURL'));
159
    //     $query_factory
160
    //         ->set('idSite', $this->siteId)
161
    //         ->set('token_auth', $this->authToken);
162
163
    //     $liveCounters = $query_factory->getQuery('Live.getCounters')
164
    //     ->setParameter('lastMinutes', 3)
165
    //     ->execute()
166
    //     ->getResponse();
167
168
    //     return response()->json([
169
    //         'success'=>'success',
170
    //         'liveCounter' => $liveCounters
171
    //     ]);
172
    // }
173
    // public function matomo(Request $request)
174
    // {
175
    //     $data = $request->all();
176
    //     $query_factory = QueryFactory::create(config('chuckcms.analytics.matomoURL'));
177
    //     $query_factory
178
    //         ->set('idSite', $this->siteId)
179
    //         ->set('token_auth', $this->authToken);
180
    //     $matomoVersion = $query_factory->getQuery('API.getMatomoVersion')->execute()->getResponse()->value;
181
182
    //     $matomo = new Matomo(config('chuckcms.analytics.matomoURL'), $this->authToken, $this->siteId, Matomo::FORMAT_JSON);
183
    //     // $matomoVersion = $matomo->getMatomoVersion();
184
    //     if($data["value"]["range"] == "Today"){
185
           
186
    //         $matomo->setPeriod(Matomo::PERIOD_DAY);
187
    //         $matomo->setDate(Matomo::DATE_TODAY);
188
    //         $matomoSummary = $matomo->getVisitsSummary();
189
    //         $matomoCountries = $matomo->getCountry();
190
    //         $matomoUniqueVisitors = $matomo->getUniqueVisitors();
191
    //         $getOSFamilies = $matomo->getOSFamilies();
192
    //         $getSearchEngines = $matomo->getSearchEngines();
193
    //     }else{
194
    //         $matomo->setRange(date('Y-m-d', mktime(0, 0, 0, $data["value"]["m2"], $data["value"]["d2"], $data["value"]["y2"])), date('Y-m-d', mktime(0, 0, 0, $data["value"]["m1"], $data["value"]["d1"], $data["value"]["y1"])));
195
    //         $matomoSummary = $matomo->setPeriod(Matomo::PERIOD_RANGE)->getVisitsSummary();
196
    //         $getOSFamilies = $matomo->getOSFamilies();
197
    //         $matomoCountries = $matomo->getCountry();
198
            
199
    //         $getSearchEngines = $matomo->getSearchEngines();
200
    //         $matomoUniqueVisitors = $matomo->setPeriod(Matomo::PERIOD_DAY)->getUniqueVisitors();
201
    //     }
202
       
203
204
    //     return response()->json([
205
    //         'success'=>'success',
206
    //         'matomoVersion' => $matomoVersion,
207
    //         'matomoSummary' => $matomoSummary,
208
    //         'matomoUniqueVisitors' => $matomoUniqueVisitors,
209
    //         'getSearchEngines' => $getSearchEngines,
210
    //         'matomoCountries' => $matomoCountries,
211
    //         'getOSFamilies' => $getOSFamilies
212
    //     ]);
213
    // }
214
215
    // public function counter(Request $request)
216
    // {
217
    //     $data = $request->all();
218
    //     $matomo = new Matomo(config('chuckcms.analytics.matomoURL'), $this->authToken, $this->siteId, Matomo::FORMAT_JSON);
219
    //     $liveCounter = $matomo->getCounters($lastMinutes = 3);
220
    //     return response()->json([
221
    //         'success'=>'success',
222
    //         'liveCounter' => $liveCounter
223
    //     ]);
224
    // }
225
226
    public function submit(Request $request)
227
    {
228
        $request->validate([
229
            'siteId' => 'required',
230
            'authtoken' => 'required'
231
        ]);
232
        $settings = [];
233
        $settings['id'] = ChuckSite::getSite('id');
234
        $settings['name'] = ChuckSite::getSite('name');
235
        $settings['slug'] = ChuckSite::getSite('slug');
236
        $settings['company'] =  ChuckSite::getSetting('company') ;
237
        $settings['socialmedia'] =  ChuckSite::getSetting('socialmedia');
238
        $settings['favicon'] = ChuckSite::getSetting('favicon');
239
        $settings['logo'] =  ChuckSite::getSetting('logo');
240
        $settings['lang'] = ChuckSite::getSetting('lang');
241
        $settings['domain'] = ChuckSite::getSetting('domain');
242
        $settings['integrations'] = [
243
            'matomo-site-id' => $request->siteId,
244
            'matomo-auth-key' => $request->authtoken
245
        ];
246
        // //update or create settings
247
        $this->siteRepository->updateIntegrations($settings);
248
249
        //redirect back
250
        return redirect()->route('dashboard.matomo')->with('notification', 'Instellingen opgeslagen!');
251
        
252
    }
253
254
}