YaWeather::show()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 4
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: ignatenkov
5
 * Date: 20.08.15
6
 * Time: 22:05
7
 */
8
9
namespace YaWeather {
10
11
12
    class YaWeather {
13
14
        private $_citiyId;
15
        private $_url;
16
        private $_error = [];
17
        private $_success = [];
18
19
        /**
20
         * Construct $id is cityId
21
         * @param int $id
22
         */
23
        public function __construct($id = 27643, $url = "https://export.yandex.ru/weather-ng/forecasts/"){
24
            $this->_citiyId = $id;
25
            $this->_url = $url;
26
        }
27
28
        /**
29
         * Function to load weather from web
30
         */
31
        public function load() {
32
33
            $url = $this->CreateUrl();
34
35
            $userAgent = 'Googlebot/2.1 (+http://www.google.com/bot.html)';
36
37
            $ch = curl_init($url);
38
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
39
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
40
            curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
41
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
42
            $output = curl_exec($ch);
43
44
            if(curl_errno($ch))
45
                $this->_error[] = "Error load weather from web. curl_error output: " .curl_error($ch).".";
46
            else
47
                $this->_success[] = "Load weather from web OK.";
48
            $this->saveToFile($output);
49
        }
50
51
        /**
52
         * Save output to File
53
         * @param $output
54
         */
55
        private function saveToFile($output) {
56
            $fileName = $this->getFileName();
57
58
            $fh = fopen($fileName, 'w');
59
60
            if(fwrite($fh, $output))
61
                $this->_success[] = "File success write.";
62
            else
63
                $this->_error[] = "Error file save.";
64
            fclose($fh);
65
        }
66
67
        /**
68
         * Get Error & Success status
69
         * @return string
70
         */
71
        public function logFormat() {
72
            $str = "";
73
74
            if($this->_error)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_error of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
75
                $str .= "Error: ".implode(" ", $this->_error)."<br>";
76
            if($this->_success)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_success of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
77
                $str .= "Success: ".implode(" ", $this->_success)."<br>";
78
79
            return $str;
80
        }
81
82
        /**
83
         * Get errors
84
         * @return array
85
         */
86
        public function getError() {
87
            return $this->_error;
88
        }
89
90
        /**
91
         * Get success
92
         * @return array
93
         */
94
        public function getSuccess(){
95
            return $this->_success;
96
        }
97
98
99
        /**
100
         * Return parse weather
101
         * @return string|Models\City
102
         */
103
        public function getResult() {
104
105
            $parse = new ParseXml($this->getFileName());
106
107
            if($parse->parse())
108
                return $parse->parse();
109
            else
110
                return false;
111
112
        }
113
114
        /**
115
         * show page html.php
116
         * @param bool $cp1251
117
         */
118
        public function show($cp1251 = false) {
0 ignored issues
show
Coding Style introduced by
show uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
119
            $file = "html.php";
120
            if($cp1251)
121
                $file = "htmlcp1251.php";
122
123
            $city = $this->getResult();
124
125
            $path = str_replace($_SERVER['DOCUMENT_ROOT'], "", __DIR__)."./../css";
0 ignored issues
show
Unused Code introduced by
$path 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...
126
127
            if($city != null)
128
                require_once(dirname(__DIR__).'/'.$file);
129
        }
130
131
        /**
132
         * Return url to load xml
133
         * @return string
134
         */
135
        private function CreateUrl() {
136
            return $this->_url . $this->_citiyId .".xml";
137
        }
138
139
        /**
140
         * Get path
141
         * @return string
142
         */
143
        public function getFileName() {
144
            return dirname(__DIR__)."/city_" . $this->_citiyId . ".xml";
145
        }
146
147
    }
148
}