UpdateServices::xml()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
4
namespace ElePHPant\UpdateServices;
5
6
7
/**
8
 * Class UpdateServices
9
 *
10
 * Please report bugs on https://github.com/wilderamorim/updateservices/issues
11
 *
12
 * @package ElePHPant\UpdateServices
13
 * @author Wilder Amorim <[email protected]>
14
 * @copyright Copyright (c) 2020, Uebi. All rights reserved
15
 * @license MIT License
16
 */
17
class UpdateServices
18
{
19
    /** @var string */
20
    private $name;
21
22
    /** @var string */
23
    private $url;
24
25
    /** @var string|null */
26
    private $rss;
27
28
    /**
29
     * @var array|null
30
     * @link https://codex.wordpress.org/Update_Services
31
     * @link https://codex.wordpress.org/pt-br:Servi%C3%A7os_de_Atualiza%C3%A7%C3%A3o
32
     */
33
    private $services = [];
34
35
    /**
36
     * UpdateServices constructor.
37
     * @param string $name
38
     * @param string $url
39
     * @param string|null $rss
40
     * @param array|null $services
41
     */
42
    public function __construct(string $name, string $url, ?string $rss = null, ?array $services = [])
43
    {
44
        $this->name = $name;
45
        $this->url = $url;
46
        $this->rss = $rss;
47
        $this->services = $services;
48
    }
49
50
    /**
51
     * @return bool|string
52
     * @link https://pingomatic.com/
53
     */
54
    public function pingOMatic()
55
    {
56
        $fields = http_build_query([
57
            'title' => $this->name,
58
            'blogurl' => $this->url,
59
            'rssurl' => $this->rss,
60
61
            //Services to Ping
62
            'chk_blogs' => 'on',        // Blo.gs (http://blo.gs/)
63
            'chk_feedburner' => 'on',   // Feed Burner (http://feedburner.com/)
64
            'chk_tailrank' => 'on',     // Spinn3r (http://spinn3r.com/)
65
            'chk_superfeedr' => 'on'    // Superfeedr (http://superfeedr.com/)
66
        ]);
67
68
        return $this->ping("https://pingomatic.com/ping/?{$fields}", $fields);
69
    }
70
71
    /**
72
     * @return bool
73
     */
74
    public function all(): bool
75
    {
76
        foreach ($this->services as $service) {
77
            $this->ping($service, $this->xml());
78
        }
79
80
        $this->pingOMatic();
81
82
        return true;
83
    }
84
85
    /**
86
     * @return mixed
87
     * @link http://blo.gs/ping-example.php
88
     */
89
    private function xml()
90
    {
91
        $xml = new \SimpleXMLIterator('<?xml version="1.0" encoding="UTF-8"?><methodCall/>');
92
        $xml->addChild('methodName', 'weblogUpdates.extendedPing');
93
        $params = $xml->addChild('params');
94
95
        foreach ([$this->name, $this->url, $this->rss] as $item) {
96
            $param = $params->addChild('param');
97
            $param->addChild('value', $item);
98
        }
99
100
        return $xml->asXML();
101
    }
102
103
    /**
104
     * @param string $url
105
     * @param string $fields
106
     * @return bool|string
107
     */
108
    private function ping(string $url, string $fields)
109
    {
110
        $ch = curl_init();
111
        curl_setopt($ch, CURLOPT_URL, $url);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
112
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
113
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
114
        curl_setopt($ch, CURLOPT_POST, 1);
115
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
116
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
117
        $response = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
        $response = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
118
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
119
120
        return $response;
121
    }
122
}