Gcalendar_plugin   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 8
dl 0
loc 106
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B answer() 0 41 5
A getPriority() 0 3 1
A isLikely() 0 3 1
A hasSession() 0 3 1
A getClient() 0 23 3
1
<?php
2
3
namespace JarvisPHP\Plugins\Gcalendar_plugin;
4
5
use JarvisPHP\Core\JarvisSession;
6
use JarvisPHP\Core\JarvisPHP;
7
use JarvisPHP\Core\JarvisLanguage;
8
use JarvisPHP\Core\JarvisTTS;
9
10
define('APPLICATION_NAME', 'JarvisPHP Client');
11
define('CREDENTIALS_PATH', 'Plugins/Gcalendar_plugin/api-key.json');
12
define('CLIENT_SECRET_PATH', 'Plugins/Gcalendar_plugin/secret-client-key.json');
13
define('SCOPES', implode(' ', array(
14
  \Google_Service_Calendar::CALENDAR_READONLY)
15
));
16
define('_MAX_EVENTS', 4);
17
18
/**
19
 * Google Calendar plugin
20
 * @author Stefano Bianchini
21
 * @website http://www.stefanobianchini.net
22
 */
23
class Gcalendar_plugin implements \JarvisPHP\Core\JarvisPluginInterface{
24
    /**
25
     * Priority of plugin
26
     * @var int  
27
     */
28
    var $priority = 1;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $priority.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
29
    
30
    /**
31
     * the behaviour of plugin
32
     * @param string $command
33
     */
34
    function answer($command) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
        $answer = '';
36
        
37
        JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"');
38
        
39
        // Get the API client and construct the service object.
40
        $client = Gcalendar_plugin::getClient();
41
        
42
        if($client==null) return null;
43
44
        $service = new \Google_Service_Calendar($client);
45
46
        // Print the next _MAX_EVENTS events on the user's calendar.
47
        $calendarId = 'primary';
48
        $optParams = array(
49
          'maxResults' => _MAX_EVENTS,
50
          'orderBy' => 'startTime',
51
          'singleEvents' => TRUE,
52
          'timeMin' => date('c'),
53
        );
54
        $results = $service->events->listEvents($calendarId, $optParams);
55
56
        if (count($results->getItems()) == 0) {
0 ignored issues
show
Bug introduced by
The method getItems() does not seem to exist on object<Google_Http_Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
          $answer = JarvisLanguage::translate('no_appointments',get_called_class());
58
        } else {
59
          
60
          foreach ($results->getItems() as $event) {
0 ignored issues
show
Bug introduced by
The method getItems() does not seem to exist on object<Google_Http_Request>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
            $start = $event->start->dateTime;
62
            if (empty($start)) {
63
              $start = $event->start->date;
64
            }
65
66
            $date = new \DateTime($start);
67
            $answer.= sprintf(JarvisLanguage::translate('list_events',get_called_class()), $date->format('j'), JarvisLanguage::translate('month_'.$date->format('n'),get_called_class()), $date->format('H'), $date->format('i'), $event->getSummary())."\n";
68
          }
69
        }
70
71
        JarvisTTS::speak($answer);
72
        $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true);
73
        $response->send();
74
    }
75
    /**
76
     * Get plugin's priority
77
     * @return int
78
     */
79
    function getPriority() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
        return $this->priority;
81
    }
82
    
83
    /**
84
     * Is it the right plugin for the command?
85
     * @param string $command
86
     * @return boolean
87
     */
88
    function isLikely($command) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
        return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return preg_match(\Jarvi...ed_class()), $command); (integer) is incompatible with the return type declared by the interface JarvisPHP\Core\JarvisPluginInterface::isLikely of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
90
    }
91
    
92
    /**
93
     * Does the plugin need a session?
94
     * @return boolean
95
     */
96
    function hasSession() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
        return false;
98
    }
99
100
    /**
101
     * Returns an authorized API client.
102
     * @return Google_Client the authorized client object
103
     */
104
    function getClient() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
105
      $client = new \Google_Client();
106
      $client->setApplicationName(APPLICATION_NAME);
107
      $client->setScopes(SCOPES);
108
      $client->setAuthConfigFile(CLIENT_SECRET_PATH);
109
      $client->setAccessType('offline');
110
111
      // Load previously authorized credentials from a file.
112
      $credentialsPath = CREDENTIALS_PATH;
113
      if (file_exists($credentialsPath)) {
114
        $accessToken = file_get_contents($credentialsPath);
115
      } else {
116
        return null;
117
      }
118
      $client->setAccessToken($accessToken);
119
120
      // Refresh the token if it's expired.
121
      if ($client->isAccessTokenExpired()) {
122
        $client->refreshToken($client->getRefreshToken());
123
        file_put_contents($credentialsPath, $client->getAccessToken());
124
      }
125
      return $client;
126
    }
127
128
}
129