Skip to content

Commit 1c06d29

Browse files
authored
Merge pull request #24 from pashaapsky/webSockets
Web sockets
2 parents c3fcfb2 + defd505 commit 1c06d29

34 files changed

+66838
-27186
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Broadcasting;
4+
5+
use App\User;
6+
7+
class AdminChatChannel
8+
{
9+
/**
10+
* Create a new channel instance.
11+
*
12+
* @return void
13+
*/
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Authenticate the user's access to the channel.
21+
*
22+
* @param \App\User $user
23+
* @return array|bool
24+
*/
25+
public function join(User $user)
26+
{
27+
return $user->hasRole('admin');
28+
}
29+
}

app/Events/ChannelEvent.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Broadcasting\PresenceChannel;
8+
use Illuminate\Broadcasting\PrivateChannel;
9+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10+
use Illuminate\Foundation\Events\Dispatchable;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class ChannelEvent implements ShouldBroadcast
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
public $user;
18+
public $data;
19+
20+
public function __construct($user, $data)
21+
{
22+
$this->user = $user;
23+
$this->data = $data;
24+
}
25+
26+
/**
27+
* Get the channels the event should broadcast on.
28+
*
29+
* @return \Illuminate\Broadcasting\Channel|array
30+
*/
31+
public function broadcastOn()
32+
{
33+
return new Channel('test-channel');
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use Illuminate\Broadcasting\InteractsWithSockets;
6+
use Illuminate\Broadcasting\PrivateChannel;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class PostUpdatedAdminChat implements ShouldBroadcast
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
public $post;
16+
public $data;
17+
18+
public function __construct($post, $data)
19+
{
20+
$this->post = $post;
21+
$this->data = $data;
22+
}
23+
24+
public function broadcastOn()
25+
{
26+
return new PrivateChannel('admin-chat-channel');
27+
}
28+
}

app/Http/Controllers/AdministrationController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\News;
66
use App\Post;
7+
use Illuminate\Http\Request;
78

89
class AdministrationController extends Controller
910
{
@@ -25,4 +26,15 @@ public function news() {
2526
$news = News::latest()->get();
2627
return view('/admin.news', compact('news'));
2728
}
29+
30+
public function orders() {
31+
return view('/admin.orders');
32+
}
33+
34+
public function ordersStore(Request $request) {
35+
36+
\App\Jobs\GenerateResultingOrder::dispatch($request->all(), auth()->user());
37+
38+
return back();
39+
}
2840
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Comment;
6+
use App\Mail\ResultingOrderSend;
7+
use App\News;
8+
use App\Post;
9+
use App\Tag;
10+
use Illuminate\Bus\Queueable;
11+
use Illuminate\Contracts\Queue\ShouldQueue;
12+
use Illuminate\Foundation\Bus\Dispatchable;
13+
use Illuminate\Queue\InteractsWithQueue;
14+
use Illuminate\Queue\SerializesModels;
15+
use Illuminate\Support\Facades\Mail;
16+
use Throwable;
17+
18+
class GenerateResultingOrder implements ShouldQueue
19+
{
20+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
21+
22+
protected $requestData;
23+
protected $user;
24+
25+
public function __construct($requestData, $user)
26+
{
27+
$this->requestData = $requestData;
28+
$this->user = $user;
29+
}
30+
31+
public function handle()
32+
{
33+
$data = [];
34+
35+
if ($this->requestData) {
36+
if (array_key_exists ( 'news' , $this->requestData )) {
37+
$data['news'] = 'Новостей: ' . News::count() . PHP_EOL;
38+
}
39+
40+
if (array_key_exists ( 'posts' , $this->requestData )) {
41+
$data['posts'] = 'Статей: ' . Post::count() . PHP_EOL;
42+
}
43+
44+
if (array_key_exists ( 'comments' , $this->requestData )) {
45+
$data['comments'] = 'Комментариев: ' . Comment::count() . PHP_EOL;
46+
}
47+
48+
if (array_key_exists ( 'tags' , $this->requestData )) {
49+
$data['tags'] = 'Тегов: ' . Tag::count() . PHP_EOL;
50+
}
51+
52+
if (array_key_exists ( 'users' , $this->requestData )) {
53+
$data['users'] = 'Пользователей: ' . News::count() . PHP_EOL;
54+
}
55+
}
56+
57+
Mail::to($this->user->email)->send(
58+
new ResultingOrderSend($data)
59+
);
60+
}
61+
62+
public function failed(Throwable $exception)
63+
{
64+
// Send user notification of failure, etc...
65+
}
66+
}

app/Listeners/AddHistoryOnUpdatePost.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Listeners;
44

55
use App\Events\PostUpdated;
6+
use App\Events\PostUpdatedAdminChat;
67

78
class AddHistoryOnUpdatePost
89
{
@@ -33,5 +34,7 @@ public function handle(PostUpdated $event)
3334
];
3435

3536
$event->post->history()->create($values);
37+
38+
event(new PostUpdatedAdminChat($event->post, $result));
3639
}
3740
}

app/Mail/ResultingOrderSend.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Mail\Mailable;
8+
use Illuminate\Queue\SerializesModels;
9+
use phpDocumentor\Reflection\Types\This;
10+
11+
class ResultingOrderSend extends Mailable
12+
{
13+
use Queueable, SerializesModels;
14+
15+
protected $data;
16+
17+
public function __construct($data)
18+
{
19+
$this->data = $data;
20+
}
21+
22+
/**
23+
* Build the message.
24+
*
25+
* @return $this
26+
*/
27+
public function build()
28+
{
29+
return $this->markdown('mail.order-send', ['data' => $this->data]);
30+
}
31+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"laravel/helpers": "^1.3",
1717
"laravel/telescope": "^4.0",
1818
"laravel/tinker": "^2.0",
19-
"laravel/ui": "^2.1"
19+
"laravel/ui": "^2.1",
20+
"predis/predis": "^1.1"
2021
},
2122
"require-dev": {
2223
"facade/ignition": "^2.0",

0 commit comments

Comments
 (0)