referencesTwitterAction::fireNewCategory()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * ****************************************************************************
4
 * references - MODULE FOR XOOPS
5
 * Copyright (c) Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
6
 *
7
 * You may not change or alter any portion of this comment or credits
8
 * of supporting developers from this source code or any supporting source code
9
 * which is considered copyrighted (c) material of the original comment or credit authors.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * @copyright         Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
15
 * @license           http://www.fsf.org/copyleft/gpl.html GNU public license
16
 * @package           references
17
 * @author            Hervé Thouzard of Instant Zero (http://www.instant-zero.com)
18
 *
19
 * ****************************************************************************
20
 */
21
22
/**
23
 * Plugin chargé de publier sur twitter des messages pour indiquer :
24
 * 1/ La création d'un nouveau produit
25
 * 2/ La publication d'un nouvel article
26
 *
27
 * @since 1.81
28
 */
29
class referencesTwitterAction extends references_action
30
{
31
    public function registerEvents()
32
    {
33
        /**
34
         * La liste des évènements traités par le plugin se présente sous la forme d'un tableau compposé comme ceci :
35
         *
36
         * Indice    Signification
37
         * ----------------------
38
         *    0        Evènement sur lequel se raccrocher (voir class/references_plugins.php::EVENT_ON_PRODUCT_CREATE
39
         *    1        Priorité du plugin (de 1 à 5)
40
         *    2        Script Php à inclure
41
         *    3        Classe à instancier
42
         *    4        Méthode à appeler
43
         */
44
        $events   = array();
45
        $events[] = array(
46
            references_plugins::EVENT_ON_REFERENCE_CREATE,
47
            references_plugins::EVENT_PRIORITY_1,
48
            basename(__FILE__),
49
            __CLASS__,
50
            'fireNewReference'
51
        );
52
        $events[] = array(
53
            references_plugins::EVENT_ON_CATEGORY_CREATE,
54
            references_plugins::EVENT_PRIORITY_1,
55
            basename(__FILE__),
56
            __CLASS__,
57
            'fireNewCategory'
58
        );
59
        return $events;
60
    }
61
62
    /**
63
     * Méthode générique chargée d'envoyer un texte sur un compte twitter avec une url
64
     *
65
     * @param string $textToSend Le texte à envoyer
66
     * @param string $mask       Le masque à utiliser
67
     * @param string $elementUrl L'url de l'élément concerné
68
     * @return string                Le texte qui a été envoyé à twitter
69
     */
70
    private function sendTextToTwitter($textToSend, $mask, $elementUrl)
71
    {
72
        if (!defined('REFERENCES_TWITTER_PLUGIN_PATH')) {
73
            define('REFERENCES_TWITTER_PLUGIN_PATH', REFERENCES_PLUGINS_PATH . 'actions' . DIRECTORY_SEPARATOR . 'twitter' . DIRECTORY_SEPARATOR);
74
        }
75
        require_once REFERENCES_TWITTER_PLUGIN_PATH . 'config.php';
76
        //require_once REFERENCES_TWITTER_PLUGIN_PATH.'twitter.php';
77
        require_once REFERENCES_TWITTER_PLUGIN_PATH . 'Twitter.class.php';
78
        require_once REFERENCES_TWITTER_PLUGIN_PATH . 'bitly.class.php';
79
        if (REFERENCES_BITLY_LOGIN == '') {
80
            return '';
81
        }
82
        $sentText    = '';
0 ignored issues
show
Unused Code introduced by
$sentText 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...
83
        $bitly       = new Bitly(REFERENCES_BITLY_LOGIN, REFERENCES_BITLY_API_KEY);
84
        $shortUrl    = $bitly->shortenSingle($elementUrl);
85
        $searches    = array('[itemname]', '[url]');
86
        $replaces    = array($textToSend, $shortUrl);
87
        $sentText    = str_replace($searches, $replaces, $mask);
88
        $totalLength = strlen($sentText);
89
        if ($totalLength > REFERENCES_TWITTER_TWIT_MAX_LENGTH) {
90
            $tooLongOf = $totalLength - REFERENCES_TWITTER_TWIT_MAX_LENGTH;
91
            $searches  = array('[itemname]', '[url]');
92
            $replaces  = array(substr($textToSend, 0, strlen($textToSend) - $tooLongOf), $shortUrl);
93
            $sentText  = str_replace($searches, $replaces, $mask);
94
        }
95
        if (trim($sentText) != '') {
96
            //          $twitter = new Twitter(REFERENCES_TWITTER_USERNAME, REFERENCES_TWITTER_PASSWORD);
97
            //          $twitter->setUserAgent('references');
98
            //          $twitter->updateStatus($sentText);
99
            $tweet = new Twitter(REFERENCES_TWITTER_USERNAME, REFERENCES_TWITTER_PASSWORD);
100
            $tweet->update($sentText);
101
        }
102
        return $sentText;
103
    }
104
105
    /**
106
     * Méthode appelée pour indiquer qu'une nouvelle référence a été publié
107
     *
108
     * @param object $parameters La référence qui vient d'être publiée
109
     * @return void
110
     */
111 View Code Duplication
    public function fireNewReference($parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
112
    {
113
        if (!defined('REFERENCES_TWITTER_PLUGIN_PATH')) {
114
            define('REFERENCES_TWITTER_PLUGIN_PATH', REFERENCES_PLUGINS_PATH . 'actions' . DIRECTORY_SEPARATOR . 'twitter' . DIRECTORY_SEPARATOR);
115
        }
116
        require_once REFERENCES_TWITTER_PLUGIN_PATH . 'config.php';
117
        $reference = $parameters['reference'];
118
        $this->sendTextToTwitter(utf8_encode($reference->getVar('article_title', 'n')), utf8_encode(REFERENCES_TWITTER_NEW_REFERENCE_INTRO), $reference->getUrl());
119
    }
120
121
    /**
122
     * Méthode appelée pour indiquer qu'une nouvelle catégorie de références a été créée
123
     *
124
     * @param object $parameters La catégorie qui vient d'être publiée
125
     * @return void
126
     */
127 View Code Duplication
    public function fireNewCategory($parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
128
    {
129
        if (!defined('REFERENCES_TWITTER_PLUGIN_PATH')) {
130
            define('REFERENCES_TWITTER_PLUGIN_PATH', REFERENCES_PLUGINS_PATH . 'actions' . DIRECTORY_SEPARATOR . 'twitter' . DIRECTORY_SEPARATOR);
131
        }
132
        require_once REFERENCES_TWITTER_PLUGIN_PATH . 'config.php';
133
        if (trim(REFERENCES_TWITTER_NEW_CATEGORY_INTRO) != '') {
134
            $category = $parameters['category'];
135
            $this->sendTextToTwitter(utf8_encode($category->getVar('category_title', 'n')), utf8_encode(REFERENCES_TWITTER_NEW_CATEGORY_INTRO), $category->getUrl());
136
        }
137
    }
138
}
139