|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Platforms; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use App\Ad; |
|
7
|
|
|
use App\Platform; |
|
8
|
|
|
use App\Platforms\Traits\ImageHelper; |
|
9
|
|
|
use App\Platforms\Traits\GetFormValidationRules; |
|
10
|
|
|
use Goutte; |
|
11
|
|
|
use Illuminate\Support\Facades\Crypt; |
|
12
|
|
|
use Illuminate\Support\Facades\Storage; |
|
13
|
|
|
use League\Uri\Components\Query; |
|
14
|
|
|
use League\Uri\Parser; |
|
15
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
16
|
|
|
|
|
17
|
|
|
class Petitesannonces implements PlatformInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use GetFormValidationRules; |
|
20
|
|
|
use ImageHelper; |
|
21
|
|
|
|
|
22
|
|
|
const MAX_IMAGE_UPLOAD_SIZE = 8388608; |
|
23
|
|
|
|
|
24
|
|
|
const BASE_URL = 'https://www.petitesannonces.ch/my'; |
|
25
|
|
|
const AD_URL = self::BASE_URL . '/annonce'; |
|
26
|
|
|
const LOGIN_URL = self::BASE_URL . '/connexion/'; |
|
27
|
|
|
const DELETE_AD_URL = self::AD_URL . '/suppression/?cids[]='; |
|
28
|
|
|
const CATEGORY_URL = self::AD_URL . '/insertion/?tid=23&step=form'; |
|
29
|
|
|
|
|
30
|
|
|
const FORM_FIELDS = [ |
|
31
|
|
|
[ |
|
32
|
|
|
'label' => 'Email', |
|
33
|
|
|
'name' => 'email', |
|
34
|
|
|
'type' => 'text', |
|
35
|
|
|
'id' => 'email', |
|
36
|
|
|
'validation' => '', |
|
37
|
|
|
], |
|
38
|
|
|
[ |
|
39
|
|
|
'label' => 'Password', |
|
40
|
|
|
'name' => 'password', |
|
41
|
|
|
'type' => 'password', |
|
42
|
|
|
'id' => 'password', |
|
43
|
|
|
'validation' => '', |
|
44
|
|
|
], |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Platform $platform |
|
49
|
|
|
*/ |
|
50
|
|
|
public function authenticate(Platform $platform) |
|
51
|
|
|
{ |
|
52
|
|
|
$config = $platform->config; |
|
53
|
|
|
|
|
54
|
|
|
$config['password'] = Crypt::decrypt($config['password']); |
|
55
|
|
|
|
|
56
|
|
|
/* @var $crawler Crawler */ |
|
57
|
|
|
$crawler = Goutte::request('GET', self::LOGIN_URL); |
|
58
|
|
|
/* @var $form \Form */ |
|
59
|
|
|
$form = $crawler->selectButton('Se connecter >')->form(); |
|
60
|
|
|
Goutte::submit($form, $config); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getFormFields(): array |
|
67
|
|
|
{ |
|
68
|
|
|
return self::FORM_FIELDS; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Ad $ad |
|
73
|
|
|
* @param Platform $platform |
|
74
|
|
|
* @return int |
|
75
|
|
|
* @throws \Exception |
|
76
|
|
|
*/ |
|
77
|
|
|
public function publish(Ad $ad, Platform $platform): int |
|
78
|
|
|
{ |
|
79
|
|
|
$this->authenticate($platform); |
|
80
|
|
|
|
|
81
|
|
|
// Select 'Other' category |
|
82
|
|
|
/* @var $crawler Crawler */ |
|
83
|
|
|
$crawler = Goutte::request('GET', self::CATEGORY_URL); |
|
84
|
|
|
|
|
85
|
|
|
// Add information about the add (title, description, price, ...) |
|
86
|
|
|
/* @var $form \Form */ |
|
87
|
|
|
$form = $crawler->selectButton('Continuer >')->form(); |
|
88
|
|
|
$adParameters = array_merge($form->getValues(), $ad->toArray()); |
|
|
|
|
|
|
89
|
|
|
$crawler = Goutte::submit($form, $adParameters); |
|
90
|
|
|
|
|
91
|
|
|
$errors = []; |
|
92
|
|
|
$crawler->filter('.fem.fieldwarning')->each(function ($node) use (&$errors) { |
|
93
|
|
|
$errors[] = $node->text(); |
|
94
|
|
|
}); |
|
95
|
|
|
|
|
96
|
|
|
if (count($errors) > 0) { |
|
97
|
|
|
throw new \Exception(implode(', ', $errors)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Get image from url |
|
101
|
|
|
$this->imageUrlValidation($ad->img_url); |
|
102
|
|
|
$file = file_get_contents($ad->img_url); |
|
103
|
|
|
$name = $this->getUniqueNameFromUrl($ad->img_url); |
|
104
|
|
|
Storage::put($name, $file); |
|
105
|
|
|
$this->imageSizeValidation($name); |
|
106
|
|
|
|
|
107
|
|
|
// Add image |
|
108
|
|
|
$form = $crawler->filter('#imageuploader')->first()->form(); |
|
109
|
|
|
$form['picture'] = Storage::path($name); |
|
110
|
|
|
$crawler = Goutte::submit($form); |
|
111
|
|
|
|
|
112
|
|
|
// Remove it since we don't need it anymore |
|
113
|
|
|
Storage::delete($name); |
|
114
|
|
|
|
|
115
|
|
|
// Accept added image(s) |
|
116
|
|
|
$form = $crawler->selectButton('Continuer >')->form(); |
|
117
|
|
|
$crawler = Goutte::submit($form, $form->getValues()); |
|
118
|
|
|
|
|
119
|
|
|
// Select free plan |
|
120
|
|
|
$form = $crawler->selectButton('Choisir')->form(); |
|
121
|
|
|
$crawler = Goutte::submit($form, ['classic' => 'Choisir']); |
|
122
|
|
|
|
|
123
|
|
|
// Confirm |
|
124
|
|
|
$form = $crawler->selectButton('Insérer mon annonce >')->form(); |
|
125
|
|
|
$crawler = Goutte::submit($form); |
|
126
|
|
|
|
|
127
|
|
|
return $this->getPublicationItemIdFromUrl($crawler->getUri()); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param Ad $ad |
|
132
|
|
|
* @param Platform $platform |
|
133
|
|
|
*/ |
|
134
|
|
|
public function unpublish(Ad $ad, Platform $platform) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->authenticate($platform); |
|
137
|
|
|
|
|
138
|
|
|
$publicationItemId = $ad->platforms()->where('platform_id', $platform->id)->first()->pivot->publication_item_id; |
|
139
|
|
|
|
|
140
|
|
|
/* @var $crawler Crawler */ |
|
141
|
|
|
$crawler = Goutte::request('GET', self::DELETE_AD_URL . $publicationItemId); |
|
142
|
|
|
/* @var $form \Form */ |
|
143
|
|
|
$form = $crawler->selectButton('Oui')->form(); |
|
144
|
|
|
Goutte::submit($form); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param $url |
|
149
|
|
|
* @return mixed |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getPublicationItemIdFromUrl($url) |
|
152
|
|
|
{ |
|
153
|
|
|
$parser = new Parser(); |
|
154
|
|
|
$queryString = $parser($url)['query']; |
|
155
|
|
|
$query = new Query($queryString); |
|
156
|
|
|
return $query->getParam('cid'); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.