Completed
Push — master ( 45de7b...71112b )
by Rasmus
04:48
created

BlogController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
namespace Chp\TextContent;
3
4
/**
5
 * A blog controller
6
 * Made by Rasmus Berg (c) 2014-2017
7
 *
8
 * @Property  Object  $this->di         Anax-MVC class handler
9
 * @Property  Object  $this->response   Anax-MVC Php Header class
10
 * @Property  Object  $this->url        Anax-MVC url-handler class
11
 * @Property  Object  $this->theme      Anax-MVC theme-handler class
12
 * @Property  Object  $this->views      Anax-MVC views-handler class
13
 * @Property  Object  $this->textFilter Anax-MVC textformat-handler class
14
 */
15
class BlogController implements \Anax\DI\IInjectionAware
16
{
17
  use \Anax\DI\TInjectable;
18
	
19
  /**
20
	 * Properties
21
	 */
22
  private $content = null;
23
  private $postsPerPage = null;
24
  private $urlPrefix = "content.php/";
25
  
26
  /**
27
   * Initialize the controller
28
   *
29
   * @Return    Void
30
   */
31 2
  public function initialize(){
32 2
    $this->content = new \Chp\TextContent\Content();
33 2
    $this->content->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\IInjectionAware::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34 2
  }
35
  
36
   /**
37
   * Index action - uses tagAction whitout tag
38
   *
39
   * @Return   Void
40
   */
41
  public function indexAction($page = null){
42
    $posts = $this->getBlogContent($page);
43
    
44
    if(count($posts) > 0){
45
      foreach($posts AS $key => $post){
46
        // Prepare blog post for show in view
47
        $posts[$key] = $this->preparePost($post);
48
      }
49
    }
50
    
51
    // Show blog posts in view
52
    $this->postsToView($posts, null);
0 ignored issues
show
Documentation introduced by
$posts is of type object, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
  }
54
  
55
  /**
56
   * Get blog posts by tag
57
   *
58
   * @Param   String    $tag    Blog-tag
59
   * @Param   Integer   $page   Page on paging
60
   * @Return  Void
61
   */
62
  public function tagAction($tag = null, $page = null){    
63
    $posts = $this->getBlogContent($page, $tag);
64
    
65
    if(count($posts) == 0 && !is_null($tag))
66
      $this->response->redirect($this->url->create($this->urlPrefix . 'blog/'));
1 ignored issue
show
Documentation introduced by
The property response does not exist on object<Chp\TextContent\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property url does not exist on object<Chp\TextContent\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
68
    foreach($posts AS $key => $post){
69
      // Prepare blog post for show in view
70
      $posts[$key] = $this->preparePost($post, $tag);
0 ignored issues
show
Unused Code introduced by
The call to BlogController::preparePost() has too many arguments starting with $tag.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
71
    }
72
    
73
    $this->postsToView($posts, $tag);
0 ignored issues
show
Documentation introduced by
$posts is of type object, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
  }
75
  
76
  /**
77
   * Get blog post by slug
78
   *
79
   * @Param   String    $slug    Blog-tag
80
   * @Return  Void
81
   */
82
  public function readAction($slug = null){
83
    
84
    if(is_null($slug)){
85
      $this->response->redirect($this->url->create($this->urlPrefix . 'blog/'));
86
    }
87
    
88
    $post = $this->content->getContentBySlug($slug, 'blog-post');
89
    
90
    if(empty($post)){
91
      $this->response->redirect($this->url->create($this->urlPrefix . 'blog/'));
92
    }
93
    
94
    // Prepare blog post for show in view
95
    $post = $this->preparePost($post);
0 ignored issues
show
Documentation introduced by
$post is of type object, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
    
97
    $title = "Blog -  {$post->title}";
98
    
99
    $this->theme->setTitle($title);
100
    $this->views->add('text-content/blog-post', 
101
      [
102
        'title'         => $title,
103
        'post' 	        => $post,
104
        'blogIndexUrl'  => $this->url->create($this->urlPrefix . 'blog/')
105
      ]
106
    );
107
    
108
    /*$this->dispatcher->forward([
109
      'controller' => 'comment',
110
      'action'     => 'view',
111
      'params'	   =>	["blog/read/{$post->slug}"]
112
    ]);*/
113
  }
114
  
115
  /**
116
   * Get blog posts by tag or type
117
   *
118
   * @Param   Int     $page    Page asked for
119
   * @Param   String  $tag     Tag-slug to search for
120
   * @Return  Array   $posts   A array with post objects
121
   */
122 1
  public function getBlogContent($page = null, $tag = null){
123 1
    if(!is_null($tag))
124 1
      return $this->content->getAllContentOfTag($tag, 'blog-post', $page, $this->postsPerPage);
125
    else
126 1
      return $this->content->getAllContentOfType('blog-post', $page, $this->postsPerPage);
127
  }
128
  
129
  /**
130
   * Prepare blog post to show in view 
131
   *
132
   * @Param   Array   $post   Blog post object
133
   * @Return  Object  $result Prepared blog post object
134
   */
135 1
  public function preparePost($post = null){    
136 1
    $result = null;
137
    
138 1
    if(!is_null($post)){
139 1
      $result = (object)[];
140
      
141 1
      foreach($post as $key => $value){
142 1
        $result->{$key} = $value;
143 1
      }
144
      
145 1
      $result->title        = htmlspecialchars($post->title, ENT_QUOTES);
146 1
      $result->ingress      = htmlspecialchars($post->ingress, ENT_QUOTES);
147 1
      $result->text         = $this->textFilter->doFilter(htmlspecialchars($post->text, ENT_QUOTES), $post->filters);
0 ignored issues
show
Documentation introduced by
The property textFilter does not exist on object<Chp\TextContent\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
148 1
      $result->editUrl      = $this->url->create("content/edit/{$post->id}");
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Chp\TextContent\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
149 1
      $result->showUrl      = $this->url->create("blog/read/" . $post->slug);
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Chp\TextContent\BlogController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
150
      //$result->authorId     = $post->author;
151
      //$result->authorName   = htmlspecialchars($post->name, ENT_QUOTES);
152
      //$result->authorUrl    = $this->url->create('users/id/' . $post->author);
153
      
154
      //unset($result->author);
155
      
156 1
      $tags = $this->content->getTagsForContent($post->id);
157
      
158 1
      foreach($tags AS $item_key => $item){
159 1
        $tags[$item_key]->tag  = htmlspecialchars($item->tag, ENT_QUOTES);
160 1
        $tags[$item_key]->url  = $this->url->create($this->urlPrefix . "blog/tag/{$item->slug}");
161 1
      }
162
      
163 1
      $result->tags = $tags;
164 1
    }
165
166 1
    return $result;
167
  }
168
  
169
  /**
170
   * Show blog posts in view
171
   *
172
   * @Param   Array   $posts   Array of blog post objects
173
   * @Param   String  $tag     Tag-slug which has give this result
174
   * @Return  Void
175
   */
176
  private function postsToView($posts = array(), $tag = null){
177
    $tag_title = null;
178
    
179
    if(!is_null($tag))
180
      $tag_title = $this->content->getTagBySlug($tag);
181
    
182
    $title = "Blog" . ((!is_null($tag_title)) ? " - tag: {$tag_title}" : NULL);
183
  
184
    $this->theme->setTitle($title);
185
    $this->views->add('text-content/blog-index', 
186
      [
187
        'title'     => $title,
188
        'posts' 	  => $posts,
189
        'tag'       => $tag_title
190
      ]
191
    );
192
  }
193
}