Reports   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 52
dl 0
loc 89
rs 10
c 5
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B getInsightsReport() 0 78 9
1
<?php
2
/**
3
 * @link      https://dukt.net/facebook/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/facebook/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\facebook\services;
9
10
use Craft;
11
use craft\base\Component;
12
use dukt\facebook\Plugin as Facebook;
13
14
15
/**
16
 * Class Reports service
17
 *
18
 * @author Dukt <[email protected]>
19
 * @since  2.0
20
 */
21
class Reports extends Component
22
{
23
    // Public Methods
24
    // =========================================================================
25
26
    /**
27
     * Returns the Insights report.
28
     *
29
     * @return array|mixed
30
     * @throws \GuzzleHttp\Exception\GuzzleException
31
     */
32
    public function getInsightsReport()
33
    {
34
        $pluginSettings = Craft::$app->getPlugins()->getPlugin('facebook')->getSettings();
35
36
        $facebookInsightsObjectId = $pluginSettings['facebookInsightsObjectId'];
37
38
        $report = Facebook::$plugin->getCache()->get(['insightsReport', $facebookInsightsObjectId]);
39
40
        if (!$report) {
41
            $token = Facebook::$plugin->getOauth()->getToken();
42
43
            if ($token !== null) {
44
                // Object
45
46
                $object = Facebook::$plugin->getApi()->get('/'.$facebookInsightsObjectId, ['metadata' => 1, 'fields' => 'name,picture']);
47
                $objectType = $object['metadata']['type'];
48
                $message = false;
49
50
                $counts = [];
51
52
                if ($objectType == 'page') {
53
                    // Insights for objects of type `page`
54
                    $supportedObject = true;
55
                    $insights = Facebook::$plugin->getApi()->getInsights($facebookInsightsObjectId, [
56
                        'query' => [
57
                            'metric' => 'page_fans,page_impressions_unique',
58
                            'since' => date('Y-m-d', strtotime('-6 day')),
59
                            'until' => date('Y-m-d', strtotime('+1 day')),
60
                        ]
61
                    ]);
62
                    foreach ($insights['data'] as $insight) {
63
                        if ($insight['name'] == 'page_fans') {
64
                            $weekTotalStart = $insight['values'][0]['value'];
65
                            $weekTotalEnd = end($insight['values'])['value'];
66
                            $weekTotal = $weekTotalEnd - $weekTotalStart;
67
                            $counts[] = [
68
                                'label' => Craft::t('facebook', 'Total likes'),
69
                                'value' => end($insight['values'])['value']
70
                            ];
71
                            $counts[] = [
72
                                'label' => Craft::t('facebook', 'Likes this week'),
73
                                'value' => $weekTotal
74
                            ];
75
                        } elseif ($insight['name'] == 'page_impressions_unique') {
76
                            if ($insight['period'] == 'week') {
77
                                $counts[] = [
78
                                    'label' => Craft::t('facebook', 'Total Reach this week'),
79
                                    'value' => end($insight['values'])['value']
80
                                ];
81
                            } elseif ($insight['period'] == 'days_28') {
82
                                $counts[] = [
83
                                    'label' => Craft::t('facebook', 'Total Reach this month'),
84
                                    'value' => end($insight['values'])['value']
85
                                ];
86
                            }
87
                        }
88
                    }
89
                } else {
90
                    $supportedObject = false;
91
                    $message = 'Insights only supports pages, please choose a different Facebook Page ID in <a href="'.UrlHelper::getUrl('facebook/settings').'">Facebook’s settings</a>.';
0 ignored issues
show
Bug introduced by
The type dukt\facebook\services\UrlHelper 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...
92
                    Craft::info("Insights not available for object type `".$objectType."`, only pages are supported.", __METHOD__);
93
                }
94
95
                $report = [
96
                    'supportedObject' => $supportedObject,
97
                    'message' => $message,
98
                    'object' => $object,
99
                    'objectType' => $objectType,
100
                    'counts' => $counts,
101
                ];
102
103
                Facebook::$plugin->getCache()->set(['insightsReport', $facebookInsightsObjectId], $report);
104
            } else {
105
                throw new Exception("Not authenticated");
0 ignored issues
show
Bug introduced by
The type dukt\facebook\services\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
106
            }
107
        }
108
109
        return $report;
110
    }
111
}
112