Radio_plugin::answer()   C
last analyzed

Complexity

Conditions 8
Paths 72

Size

Total Lines 66
Code Lines 45

Duplication

Lines 40
Ratio 60.61 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 40
loc 66
rs 6.7082
cc 8
eloc 45
nc 72
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace JarvisPHP\Plugins\Radio_plugin;
4
5
use JarvisPHP\Core\JarvisSession;
6
use JarvisPHP\Core\JarvisPHP;
7
use JarvisPHP\Core\JarvisLanguage;
8
use JarvisPHP\Core\JarvisTTS;
9
10
/**
11
 * Radio_plugin
12
 *
13
 * @author Stefano Bianchini
14
 */
15
class Radio_plugin implements \JarvisPHP\Core\JarvisPluginInterface {
16
    /**
17
     * Priority of plugin
18
     * @var int  
19
     */
20
    var $priority = 5;
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...
21
        
22
    /**
23
     * the behaviour of plugin
24
     * @param string $command
25
     */
26
    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...
27
        
28
        $start_vlc = false;
29
        $stream_url='';
30
        $answer='nothing done';
31
        
32
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
33
                    
34 View Code Duplication
        if(preg_match(JarvisLanguage::translate('preg_match_relax_music',get_called_class()), $command)) { 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
            $stream_url = 'http://pub4.radiotunes.com:80/radiotunes_relaxation_aacplus.flv';
36
            JarvisPHP::getLogger()->debug('Playing relaxation');
37
            $start_vlc = true;
38
        }
39 View Code Duplication
        if(preg_match(JarvisLanguage::translate('preg_match_lounge_music',get_called_class()), $command)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
            $stream_url = 'http://pub4.radiotunes.com:80/radiotunes_smoothlounge_aacplus.flv';
41
            JarvisPHP::getLogger()->debug('Playing lounge');
42
            $start_vlc = true;
43
        }
44 View Code Duplication
        if(preg_match(JarvisLanguage::translate('preg_match_jazz_music',get_called_class()), $command)) { 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
            $stream_url = 'http://pub4.radiotunes.com:80/radiotunes_smoothjazz_aacplus.flv';
46
            JarvisPHP::getLogger()->debug('Playing jazz');
47
            $start_vlc = true;
48
        }
49
        if(preg_match(JarvisLanguage::translate('preg_match_stop',get_called_class()), $command)) {
50
            $result = @socket_connect($socket, 'localhost', 9999);
51 View Code Duplication
            if ($result === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
                //Error handling
53
                JarvisPHP::getLogger()->warn('Failed to connect!');
54
                $answer = 'Failed to connect!';
55
            }
56
            else {
57
                $vlc_remote_command = 'stop'.chr(13);
0 ignored issues
show
Unused Code introduced by
$vlc_remote_command is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
                $vlc_remote_command = 'quit'.chr(13);
59
                socket_write($socket, $vlc_remote_command, strlen($vlc_remote_command));
60
                socket_close($socket);
61
                JarvisPHP::getLogger()->debug('Radio stopped');
62
                $answer = 'Radio stopped';
63
            }
64
            $start_vlc = false;    
65
        }
66
        if($start_vlc) {
67
            //Start Vlc on port 9999
68
            //exec('vlc -I rc --rc-host localhost:9999');
69
            @exec('/usr/bin/nohup /usr/bin/vlc -I rc --rc-host localhost:9999 &');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
70
71
            sleep(1);
72
73
            $result = @socket_connect($socket, 'localhost', 9999);
74 View Code Duplication
            if ($result === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
                //Error handling
76
                JarvisPHP::getLogger()->warn('Failed to connect!');
77
                $answer = 'Failed to connect!';
78
            }
79
            else {
80
                $vlc_remote_command = 'add '.$stream_url.chr(13);
81
                socket_write($socket, $vlc_remote_command, strlen($vlc_remote_command));
82
                socket_close($socket);
83
                JarvisPHP::getLogger()->debug('Radio started');
84
                $answer = 'Radio started';
85
            }
86
        }
87
        
88
        $response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true);
89
        $response->send();
90
        
91
    }
92
    
93
    /**
94
     * Get plugin's priority
95
     * @return int
96
     */
97
    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...
98
        return $this->priority;
99
    }
100
    
101
    /**
102
     * Is it the right plugin for the command?
103
     * @param string $command
104
     * @return boolean
105
     */
106
    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...
107
        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...
108
    }
109
    
110
    /**
111
     * Does the plugin need a session?
112
     * @return boolean
113
     */
114
    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...
115
        return false;
116
    }
117
}
118