Completed
Push — master ( b88b55...5b50de )
by Anthony
02:03
created

AdminArticle   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 119
Duplicated Lines 22.69 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
c 1
b 0
f 1
lcom 1
cbo 2
dl 27
loc 119
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getTestTitle() 0 22 4
A getTestArticle() 0 8 2
B getAllArticle() 27 27 4
A setAddArticle() 0 21 3

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
	namespace modules\blog\admin\controller;
3
	
4
	
5
	use core\App;
6
	use core\functions\ChaineCaractere;
7
	use core\HTML\flashmessage\FlashMessage;
8
	use modules\blog\app\controller\Blog;
9
	
10
	class AdminArticle {
11
		private $error_title;
12
		private $error_article;
13
		
14
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
15
		public function __construct() {
16
			
17
		}
18
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
19
		
20
		
21
		//-------------------------- GETTER ----------------------------------------------------------------------------//
22
		/**
23
		 * @param $title
24
		 * @return bool
25
		 * function that verify if title of the article is ok
26
		 */
27
		private function getTestTitle($title) {
28
			$dbc = App::getDb();
29
			
30
			if (ChaineCaractere::testMinLenght($title, 4) == false) {
31
				$this->error_title = "votre titre doit être supérieur à 4 caractères";
32
				return false;
33
			}
34
			
35
			if (strlen($title) > 20) {
36
				$this->error_title = "votre titre ne doit pas eccéder 20 caractères";
37
				return false;
38
			}
39
			
40
			$query = $dbc->select()->from("_blog_article")->where("title", "=", $title)->get();
41
			
42
			if (count($query) > 0) {
43
				$this->error_title = "votre titre existe déjà merci d'en choisir un autre";
44
				return false;
45
			}
46
			
47
			return true;
48
		}
49
		
50
		/**
51
		 * @param $article
52
		 * @return bool
53
		 * function that verify if article is ok
54
		 */
55
		private function getTestArticle($article) {
56
			if (ChaineCaractere::testMinLenght($article, 10) == false) {
57
				$this->error_article = "votre article doit être supérieur à 10 caractères";
58
				return false;
59
			}
60
			
61
			return true;
62
		}
63
		
64
		/**
65
		 * this function get last articles
66
		 */
67 View Code Duplication
		public function getAllArticle() {
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...
68
			$dbc = App::getDb();
69
			
70
			$query = $dbc->select()
71
				->from("_blog_article")
72
				->from("identite")
73
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
74
				->get();
75
			
76
			if ((is_array($query)) && (count($query) > 0)) {
77
				$articles = [];
78
				
79
				foreach ($query as $obj) {
80
					$articles[] = [
81
						"id_article" => $obj->ID_article,
82
						"title" => $obj->title,
83
						"url" => $obj->url,
84
						"article" => $obj->article,
85
						"pseudo" => $obj->pseudo,
86
						"publication_date" => $obj->publication_date,
87
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
88
					];
89
				}
90
				
91
				Blog::setValues(["articles" => $articles]);
92
			}
93
		}
94
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
95
		
96
		
97
		//-------------------------- SETTER ----------------------------------------------------------------------------//
98
		/**
99
		 * @param $title
100
		 * @param $categories
101
		 * @param $article
102
		 * @param $state
103
		 * @return bool
104
		 * function to add an article and his categories
105
		 */
106
		public function setAddArticle($title, $categories, $article, $state) {
107
			$dbc = App::getDb();
108
			
109
			if ($this->getTestTitle($title) == false || $this->getTestArticle($article) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
110
				FlashMessage::setFlash($this->error_title.$this->error_article);
111
				return false;
112
			}
113
			
114
			$dbc->insert("title", $title)
115
				->insert("url", ChaineCaractere::setUrl($title))
116
				->insert("publication_date", date("Y-m-d H:i:s"))
117
				->insert("article", $article)
118
				->insert("ID_identite", $_SESSION['idlogin'.CLEF_SITE])
119
				->insert("ID_state", $state)
120
				->into("_blog_article")->set();
121
			
122
			$id_article = $dbc->lastInsertId();
123
			
124
			AdminBlog::getAdminCategory()->setCategoriesArticle($categories, $id_article);
125
			return true;
126
		}
127
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
128
	}