1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Article\Mapper; |
6
|
|
|
|
7
|
|
|
use Article\Entity\ArticleType; |
8
|
|
|
use Zend\Db\Adapter\Adapter; |
9
|
|
|
use Zend\Db\Adapter\AdapterAwareInterface; |
10
|
|
|
use Zend\Db\TableGateway\AbstractTableGateway; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ArticlePostsMapper. |
14
|
|
|
*/ |
15
|
|
|
class ArticlePostsMapper extends AbstractTableGateway implements AdapterAwareInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $table = 'article_posts'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Db adapter setter method,. |
24
|
|
|
* |
25
|
|
|
* @param Adapter $adapter db adapter |
26
|
|
|
* |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function setDbAdapter(Adapter $adapter) |
30
|
|
|
{ |
31
|
|
|
$this->adapter = $adapter; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getPaginationSelect() |
35
|
|
|
{ |
36
|
|
|
return $this->getSql()->select() |
37
|
|
|
->columns(['title', 'body', 'lead', 'featured_img', 'main_img']) |
38
|
|
|
->join('articles', 'article_posts.article_uuid = articles.article_uuid') |
39
|
|
|
->join('category', 'articles.category_uuid = category.category_uuid', ['category_name' => 'name'], 'left') |
40
|
|
|
->where(['articles.type' => ArticleType::POST]) |
41
|
|
|
->order(['created_at' => 'desc']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function get($id) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$select = $this->getSql()->select() |
47
|
|
|
->columns(['title', 'body', 'lead', 'featured_img', 'main_img', 'has_layout']) |
48
|
|
|
->join('articles', 'article_posts.article_uuid = articles.article_uuid') |
49
|
|
|
->join( |
50
|
|
|
'category', 'category.category_uuid = articles.category_uuid', |
51
|
|
|
['category_slug' => 'slug', 'category_name' => 'name', 'category_id'], 'left' |
52
|
|
|
) |
53
|
|
|
->join('admin_users', 'admin_users.admin_user_uuid = articles.admin_user_uuid', ['admin_user_id'], 'left') |
54
|
|
|
->where(['articles.article_id' => $id]); |
55
|
|
|
|
56
|
|
|
return $this->selectWith($select)->current(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getNear($publishedAt, $direction) |
60
|
|
|
{ |
61
|
|
|
$select = $this->getSql()->select() |
62
|
|
|
->join('articles', 'article_posts.article_uuid = articles.article_uuid') |
63
|
|
|
->join('category', 'category.category_uuid = articles.category_uuid', ['category_slug' => 'slug']) |
64
|
|
|
->where(['articles.status' => 1]) |
65
|
|
|
->limit(1); |
66
|
|
|
|
67
|
|
|
if ($direction > 0) { |
68
|
|
|
$select->where->greaterThan('published_at', $publishedAt); |
69
|
|
|
$select->order(['published_at' => 'asc']); |
70
|
|
|
} elseif ($direction < 0) { |
71
|
|
|
$select->where->lessThan('published_at', $publishedAt); |
72
|
|
|
$select->order(['published_at' => 'desc']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->selectWith($select)->current(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function getBySlug($slug) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$select = $this->getSql()->select() |
81
|
|
|
->columns(['title', 'body', 'lead', 'featured_img', 'main_img']) |
82
|
|
|
->join('articles', 'article_posts.article_uuid = articles.article_uuid') |
83
|
|
|
->join( |
84
|
|
|
'category', |
85
|
|
|
'category.category_uuid = articles.category_uuid', |
86
|
|
|
['category_name' => 'name', 'category_slug' => 'slug'] |
87
|
|
|
)->join( |
88
|
|
|
'admin_users', |
89
|
|
|
'admin_users.admin_user_uuid = articles.admin_user_uuid', |
90
|
|
|
['first_name', 'last_name']) |
91
|
|
|
->where(['articles.slug' => $slug, 'articles.status' => 1]); |
92
|
|
|
|
93
|
|
|
return $this->selectWith($select)->current(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getAll() |
97
|
|
|
{ |
98
|
|
|
$select = $this->getSql()->select() |
99
|
|
|
->join('articles', 'article_posts.article_uuid = articles.article_uuid', ['article_id', 'slug']); |
100
|
|
|
|
101
|
|
|
return $this->selectWith($select); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function getLatest($limit = 10) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$select = $this->getSql()->select() |
107
|
|
|
->join('articles', 'article_posts.article_uuid = articles.article_uuid') |
108
|
|
|
->join('admin_users', 'admin_users.admin_user_uuid = articles.admin_user_uuid', ['first_name', 'last_name']) |
109
|
|
|
->join( |
110
|
|
|
'category', 'category.category_uuid = articles.category_uuid', |
111
|
|
|
['category_name' => 'name', 'category_id', 'category_slug' => 'slug'] |
112
|
|
|
) |
113
|
|
|
->where(['articles.status' => 1]) |
114
|
|
|
->order(['articles.published_at' => 'desc']) |
115
|
|
|
->limit($limit); |
116
|
|
|
|
117
|
|
|
return $this->selectWith($select); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.