Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW14 rework2 #502

Open
wants to merge 1 commit into
base: VMeshavkin/hw15
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions app/Console/Commands/Messages/MessageToAdverts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace App\Console\Commands\Messages;

use App\Services\Messages\Handler\AddMessageHandler;
use App\Services\Messages\Handler\AddMessageToAnyAdvertsHandler;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Validator;

class MessageToAdverts extends Command
{
const USER = 2;
/**
* The name and signature of the console command.
*
Expand All @@ -23,28 +26,30 @@ class MessageToAdverts extends Command
*/
protected $description = 'Add message to any adverts';
/**
* @var AddMessageHandler
* @var AddMessageToAnyAdvertsHandler
*/
private $addMessageHandler;
private $addMessageToAnyAdvertsHandler;

/**
* Create a new command instance.
*
* @param AddMessageHandler $addMessageHandler
* @param AddMessageToAnyAdvertsHandler $addMessageToAnyAdvertsHandler
*/


public function __construct(AddMessageHandler $addMessageHandler)
public function __construct(AddMessageToAnyAdvertsHandler $addMessageToAnyAdvertsHandler)
{

parent::__construct();
$this->addMessageHandler = $addMessageHandler;

$this->addMessageToAnyAdvertsHandler = $addMessageToAnyAdvertsHandler;
}

/**
* Execute the console command.
*
* @return int
* @return void
* @throws Exception
*/

public function handle()
Expand All @@ -53,8 +58,35 @@ public function handle()
$adverts = $this->argument('adverts');
$isAdmin = $this->option('admin');

$this->addMessageHandler->addMessageToAnyAdverts($message, $adverts, $isAdmin);
if ($isAdmin==0) $isAdmin = self::USER ;

$this->validate($message, $adverts, $isAdmin );

$this->addMessageToAnyAdvertsHandler->handle($message, $adverts, $isAdmin);

}

private function validate($message, $adverts, $isAdmin )
{
$input = [
'message' => $message,
'adverts' => $adverts,
'isAdmin' => $isAdmin,
];

$rules = [
'message' => 'required|max:200',
'adverts' => 'array|required',
'adverts.*'=>'integer',
'isAdmin' => 'in:0,1',
];

$validation = Validator::make($input, $rules);

if ($validation->fails())
{
throw new Exception('Not valid command params'.PHP_EOL);
}
}

}
71 changes: 0 additions & 71 deletions app/Services/Messages/Handler/AddMessageHandler.php

This file was deleted.

43 changes: 43 additions & 0 deletions app/Services/Messages/Handler/AddMessageToAnyAdvertsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php


namespace App\Services\Messages\Handler;

use App\Services\Messages\MessagesService;


class AddMessageToAnyAdvertsHandler
{
/**
* @var MessagesService
*/
private $messageService;

public function __construct(MessagesService $messageService )
{
$this->messageService = $messageService;
}

public function handle($message, $adverts, $isAdmin)
{
foreach ($adverts as $advert)
{
$data = $this->prepareData($isAdmin, $advert, $message);
$this->messageService->storeMessage($data);
}
}


private function prepareData($isAdmin, $advert, $message)
{
return $data =
[
'user_id'=>$isAdmin,
'advert_id'=>$advert,
'content'=>$message,
];
}



}