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

PageController::pageAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 15
cp 0
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
crap 12
1
<?php
2
namespace Chp\TextContent;
3
4
/**
5
 * A page controller
6
 * Made by Rasmus Berg (c) 2014-2017
7
 *
8
 * @Property  Object  $this->di         Anax-MVC class handler
9
 * @Property  Object  $this->request    Anax-MVC $_POST, $_GET and $_SERVER handler 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 PageController implements \Anax\DI\IInjectionAware
16
{
17
  use \Anax\DI\TInjectable;
18
  
19
  /**
20
	 * Properties
21
	 */
22
  private $content = null;
23
  private $urlPrefix = "content.php/";
24
  
25
  /**
26
   * Initialize the controller
27
   *
28
   * @Return    Void
29
   */
30
  public function initialize(){
31
    $this->content = new \Chp\TextContent\Content();
32
    $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...
33
  }
34
  
35
  /**
36
   * Index content - Redirect to startpage
37
   *
38
   * @Return    Void
39
   */
40
  public function indexAction(){
41
    $url = $this->request->getGet('url');
1 ignored issue
show
Documentation introduced by
The property request does not exist on object<Chp\TextContent\PageController>. 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...
42
    $this->pageAction($url);
43
  }
44
  
45
  /**
46
   * Visit page made by database content 
47
   *
48
   * @Param   String    $url    Select url to content  
49
   * @Return  Void
50
   */
51
  public function pageAction($url = null){
52
    if(is_null($url))
53
      throw new \Anax\Exception\NotFoundException();
54
    
55
    $page = $this->content->getContentByUrl($url, 'page');
56
    
57
    if(empty($page))
58
      throw new \Anax\Exception\NotFoundException();
59
    
60
    // Prepare page for show in view
61
    $page = $this->preparePage($page);
62
    
63
    $title = $page->title;
64
    
65
    $this->theme->setTitle($title);
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Chp\TextContent\PageController>. 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...
66
    $this->views->addString($title, 'page-title');
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Chp\TextContent\PageController>. 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
    $this->views->add('text-content/page', 
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Chp\TextContent\PageController>. 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...
68
      [
69
        'title'         => $title,
70
        'page' 	        => $page
71
      ]
72
    );
73
  }
74
  
75
  /**
76
   * Prepare page to show in view 
77
   *
78
   * @Param   Object    $page    Page information  
79
   * @Return  Object    $result  Prepared page information
80
   */
81 1
  public function preparePage($page = null){
82 1
    $result = null;
83
    
84 1
    if(!is_null($page)){
85 1
      $result = (object)[];
86
      
87 1
      foreach($page as $key => $value){
88 1
        $result->{$key} = $value;
89 1
      }
90
      
91 1
      $result->title         = htmlspecialchars($page->title, ENT_QUOTES);
92 1
      $result->ingress       = htmlspecialchars($page->ingress, ENT_QUOTES);
93 1
      $result->text          = $this->textFilter->doFilter(htmlentities($page->text, ENT_QUOTES), $page->filters);
1 ignored issue
show
Documentation introduced by
The property textFilter does not exist on object<Chp\TextContent\PageController>. 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...
94 1
      $result->editUrl       = $this->url->create("content/edit/{$page->id}");
1 ignored issue
show
Documentation introduced by
The property url does not exist on object<Chp\TextContent\PageController>. 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...
95
      //$result->authorId      = $author;
96
      //$result->authorName    = htmlentities($page->name, ENT_QUOTES);
97
      //$result->authorUrl     = $this->url->create('users/id/' . $page->author);
98
      
99
      //unset($author);
100 1
    }
101
    
102 1
    return $result;
103
  }
104
}