GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 61aacd...05f06e )
by Alexey
18:53
created

BaseXml   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 154
Duplicated Lines 27.92 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 2
cbo 1
dl 43
loc 154
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
buildXml() 0 1 ?
B init() 5 23 4
A render() 0 8 1
A saveXml() 12 12 2
A getTemplatePath() 16 16 3
A getXmlPath() 10 10 2
A followBuffer() 0 5 2
A flushItemsBuffer() 0 5 1
A increaseBufferCounter() 0 4 1
B isInvalidateCache() 0 15 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Alexey Tatarinov <[email protected]>
4
 * @link https://github.com/shogodev/argilla/
5
 * @copyright Copyright &copy; 2003-2015 Shogo
6
 * @license http://argilla.ru/LICENSE
7
 */
8
abstract class BaseXml extends CComponent
9
{
10
  public $templatesAlias = 'frontend.views.xml';
11
12
  public $charset = 'windows-1251';
13
14
  public $template;
15
16
  public $filePath;
17
18
  public $dataProviderClass;
19
20
  /**
21
   * @var CDbCriteria
22
   */
23
  public $criteria;
24
25
  public $itemsBufferSize = 1000;
26
27
  /**
28
   * @var XMLWriter
29
   */
30
  protected $xmlWriter;
31
32
  protected $dataProvider;
33
34
  /**
35
   * @var bool $outCashedXml отдавать кэшированый файл
36
   */
37
  protected $outCashedXml = false;
38
39
  private $itemsBufferCounter = 0;
40
41
  abstract public function buildXml();
42
43
  public function init()
44
  {
45
    set_time_limit(0);
46
47
    $this->filePath = $this->getXmlPath();
48
49
    if( !$this->isInvalidateCache() )
50
    {
51
      $this->outCashedXml = true;
52
53
      return;
54
    }
55
56
    ignore_user_abort(true);
57
    $this->xmlWriter = new XMLWriter();
58
    $this->xmlWriter->openURI($this->filePath);
59
60 View Code Duplication
    if( isset($this->dataProviderClass) )
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...
61
    {
62
      $this->criteria = isset($this->criteria) ? $this->criteria : new CDbCriteria();
63
      $this->dataProvider = new $this->dataProviderClass($this->criteria);
64
    }
65
  }
66
67
  public function render()
68
  {
69
    header('Content-Type: text/xml; charset='.$this->charset);
70
71
    readfile($this->filePath);
72
73
    Yii::app()->end();
74
  }
75
76 View Code Duplication
  protected function saveXml()
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...
77
  {
78
    $dir = pathinfo($this->filePath, PATHINFO_DIRNAME);
79
    if( !file_exists($dir) )
80
    {
81
      mkdir($dir);
82
      chmod($dir, 0775);
83
    }
84
85
    $this->xmlWriter->flush();
86
    @chmod($this->filePath, 0775);
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...
87
  }
88
89
  /**
90
   * @param string $template
91
   *
92
   * @return string $path
93
   * @throws InvalidArgumentException
94
   */
95 View Code Duplication
  protected function getTemplatePath($template)
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...
96
  {
97
    if( $template === null )
98
    {
99
      $template = str_replace("Xml", "", lcfirst(get_called_class()));
100
    }
101
102
    $path = Yii::getPathOfAlias($this->templatesAlias.'.'.$template).'.php';
103
104
    if( !file_exists($path) )
105
    {
106
      throw new InvalidArgumentException('Отсутствует файл шаблона '.$path);
107
    }
108
109
    return $path;
110
  }
111
112
  /**
113
   * @return string $filePath
114
   */
115 View Code Duplication
  protected function getXmlPath()
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...
116
  {
117
    if( !isset($this->filePath) )
118
    {
119
      $class = str_replace('DataProvider', '', $this->dataProviderClass);
120
      $this->filePath = 'f/xml/'.lcfirst($class).'.xml';
121
    }
122
123
    return $this->filePath;
124
  }
125
126
  /**
127
   * Следить за буфером, при необходимост и обнулить
128
   */
129
  protected function followBuffer()
130
  {
131
    if( $this->increaseBufferCounter() == $this->itemsBufferSize )
132
      $this->flushItemsBuffer();
133
  }
134
135
  private function flushItemsBuffer()
136
  {
137
    $this->xmlWriter->flush();
138
    $this->itemsBufferSize = 0;
139
  }
140
141
  private function increaseBufferCounter()
142
  {
143
    return $this->itemsBufferCounter++;
144
  }
145
146
  private function isInvalidateCache()
147
  {
148
    if( Yii::app()->request->getQuery('force') === 'force'  )
149
      return true;
150
151
    if( file_exists($this->filePath) )
152
    {
153
      if( !empty($this->cacheDurationInSeconds) && (time() - filemtime($this->filePath) > $this->cacheDurationInSeconds) )
154
        return true;
155
156
      return false;
157
    }
158
159
    return true;
160
  }
161
}