SugarCronRemoteJobs::getMyId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
/*********************************************************************************
4
 * SugarCRM Community Edition is a customer relationship management program developed by
5
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6
7
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
8
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
9
 *
10
 * This program is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU Affero General Public License version 3 as published by the
12
 * Free Software Foundation with the addition of the following permission added
13
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
14
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
15
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
16
 *
17
 * This program is distributed in the hope that it will be useful, but WITHOUT
18
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
20
 * details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License along with
23
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
24
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
 * 02110-1301 USA.
26
 *
27
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
28
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
29
 *
30
 * The interactive user interfaces in modified source and object code versions
31
 * of this program must display Appropriate Legal Notices, as required under
32
 * Section 5 of the GNU Affero General Public License version 3.
33
 *
34
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
35
 * these Appropriate Legal Notices must retain the display of the "Powered by
36
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
37
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
38
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
39
 ********************************************************************************/
40
41
require_once 'include/SugarQueue/SugarCronJobs.php';
42
require_once 'include/SugarHttpClient.php';
43
44
/**
45
 * CRON driver for job queue that ships jobs outside
46
 * @api
47
 */
48
class SugarCronRemoteJobs extends SugarCronJobs
49
{
50
    /**
51
     * URL for remote job server
52
     * @var string
53
     */
54
    protected $jobserver;
55
56
    /**
57
     * Just in case we'd ever need to override...
58
     * @var string
59
     */
60
    protected $submitURL = "submitJob";
61
62
    /**
63
     * REST client
64
     * @var string
65
     */
66
    protected $client;
67
68
    public function __construct()
69
    {
70
        parent::__construct();
71
        if(!empty($GLOBALS['sugar_config']['job_server'])) {
72
            $this->jobserver = $GLOBALS['sugar_config']['job_server'];
73
        }
74
        $this->setClient(new SugarHttpClient());
75
    }
76
77
    /**
78
    * Set client to talk to SNIP
79
    * @param SugarHttpClient $client
80
    */
81
    public function setClient(SugarHttpClient $client)
82
    {
83
        $this->client = $client;
84
        return $this;
85
    }
86
87
    /**
88
     * Return ID for this client
89
     * @return string
90
     */
91
    public function getMyId()
92
    {
93
        return 'CRON'.$GLOBALS['sugar_config']['unique_key'].':'.md5($this->jobserver);
94
    }
95
96
    /**
97
     * Execute given job
98
     * @param SchedulersJob $job
99
     */
100
    public function executeJob($job)
101
    {
102
        $data = http_build_query(array("data" => json_encode(array("job" => $job->id, "client" => $this->getMyId(), "instance" => $GLOBALS['sugar_config']['site_url']))));
103
        $response = $this->client->callRest($this->jobserver.$this->submitURL, $data);
0 ignored issues
show
Bug introduced by
The method callRest cannot be called on $this->client (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
104
        if(!empty($response)) {
105
            $result = json_decode($response, true);
106
            if(empty($result) || empty($result['ok']) || $result['ok'] != $job->id) {
107
                $GLOBALS['log']->debug("CRON Remote: Job {$job->id} not accepted by server: $response");
108
                $this->jobFailed($job);
109
                $job->failJob("Job not accepted by server: $response");
110
            }
111
        } else {
112
            $GLOBALS['log']->debug("CRON Remote: REST request failed for job {$job->id}");
113
            $this->jobFailed($job);
114
            $job->failJob("Could not connect to job server");
115
        }
116
    }
117
118
}
119
120