Skip to content

Commit 9831557

Browse files
committed
sample project
0 parents  commit 9831557

22 files changed

+621
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
.idea
3+
/vendor
4+
composer.lock
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Domains\Subscriptions\Entities;
4+
5+
use App\Illuminate\Database\Model;
6+
7+
class Customer extends Model
8+
{
9+
public const STATUS_ACTIVE = 1;
10+
public const STATUS_INACTIVE = 0;
11+
12+
/**
13+
* @var array|int[]
14+
*/
15+
public static array $status = [
16+
'ACTIVE' => self::STATUS_ACTIVE,
17+
'INACTIVE' => self::STATUS_INACTIVE,
18+
];
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Domains\Subscriptions\Entities;
4+
5+
use App\Illuminate\Database\Model;
6+
7+
class Plan extends Model
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Domains\Subscriptions\Entities;
4+
5+
use App\Illuminate\Database\Model;
6+
7+
class Subscription extends Model
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace App\Domains\Subscriptions\Services;
4+
5+
use App\Domains\Subscriptions\Validations\Customer\CreateNewCustomerValidation;
6+
use App\Domains\Subscriptions\Entities\Customer;
7+
use App\Support\Services\ServiceInterface;
8+
use App\Support\Validation\ValidationException;
9+
10+
class CreateNewCustomer implements ServiceInterface
11+
{
12+
private ?string $name;
13+
private ?string $lastname;
14+
private ?string $email;
15+
private ?string $status;
16+
17+
/**
18+
* CreateNewCustomer constructor.
19+
*
20+
* TIP:
21+
* Setting up all variable as null allow you to treat it in Validation layer.
22+
* on this way, you doesn't have anyone problem with data origin passed to the args.
23+
*
24+
* That means that, you can pass value from a array (with non existing key, by example) or
25+
* from a magic property from a request, etc.
26+
*
27+
* @param string|null $name
28+
* @param string|null $lastname
29+
* @param string|null $email
30+
* @param string|null $status
31+
*/
32+
public function __construct(
33+
string $name = null,
34+
string $lastname = null,
35+
string $email = null,
36+
string $status = null
37+
) {
38+
$this->name = $name;
39+
$this->lastname = $lastname;
40+
$this->email = $email;
41+
$this->status = $status;
42+
}
43+
44+
/**
45+
* @return array
46+
*
47+
* @throws ValidationException
48+
*/
49+
public function handle(): array
50+
{
51+
$data = [
52+
'name' => $this->name,
53+
'lastname' => $this->lastname,
54+
'email' => $this->email,
55+
'status' => $this->status
56+
];
57+
58+
// Validate...
59+
(new CreateNewCustomerValidation($data))->validate();
60+
61+
// store...
62+
$customer = new Customer();
63+
$customer->name = $this->name;
64+
$customer->lastname = $this->lastname;
65+
$customer->email = $this->email;
66+
// we can create a ValueObject to work with the customer status.
67+
$customer->status = Customer::$status[$this->status];
68+
$customer->save();
69+
70+
// trigger a event when a new customer was created...
71+
// event(new NewCustomerCreated($customer));
72+
73+
// grants that a array will be returned instead of a Model instance.
74+
// After creation of the new customer, what you need, a Model instance or the model data?
75+
// Probably you will need the data.
76+
return $customer->toArray();
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Domains\Subscriptions\Services;
4+
5+
use App\Domains\Subscriptions\Entities\Subscription;
6+
7+
class CreateNewSubscription
8+
{
9+
/**
10+
* @var int
11+
*/
12+
private int $customerId;
13+
/**
14+
* @var int
15+
*/
16+
private int $planId;
17+
18+
/**
19+
* Create a new subscription constructor.
20+
*
21+
* @param $customerId
22+
* @param $planId
23+
*/
24+
public function __construct(int $customerId, int $planId)
25+
{
26+
$this->customerId = $customerId;
27+
$this->planId = $planId;
28+
}
29+
30+
public function handle()
31+
{
32+
// validate...
33+
// ...
34+
35+
// Begin transaction...
36+
$subscription = new Subscription();
37+
$subscription->customer_id = $this->customerId;
38+
$subscription->plan_id = $this->planId;
39+
$subscription->save();
40+
// end transaction...
41+
42+
// Trigger a event to do some other thing...
43+
//event(new SubscriptionCreated($subscription));
44+
45+
return $subscription->toArray();
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Domains\Subscriptions\Services;
4+
5+
use App\Support\Services\ServiceInterface;
6+
use App\Support\Validation\ValidationException;
7+
8+
class CreateNewSubscriptionWithNewCustomer implements ServiceInterface
9+
{
10+
private array $customer;
11+
private ?int $planId;
12+
13+
/**
14+
* Create a enw subscription with new Customer too.
15+
*
16+
* @param array $customer
17+
* @param int|null $planId
18+
*/
19+
public function __construct(array $customer = [], int $planId = null)
20+
{
21+
$this->customer = $customer;
22+
$this->planId = $planId;
23+
}
24+
25+
/**
26+
* @return array
27+
* @throws ValidationException
28+
*/
29+
public function handle(): array
30+
{
31+
$customer = (new CreateNewCustomer(
32+
$this->customer['name'],
33+
$this->customer['lastname'],
34+
$this->customer['email'],
35+
$this->customer['status']
36+
))->handle();
37+
38+
return (new CreateNewSubscription(
39+
$customer->id,
40+
$this->planId
41+
))->handle();
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Domains\Subscriptions\Services;
4+
5+
use App\Domains\Subscriptions\Entities\Customer;
6+
use App\Support\Services\ServiceInterface;
7+
8+
class ListAllCustomers implements ServiceInterface
9+
{
10+
/**
11+
* @var string|null
12+
*/
13+
protected ?string $status;
14+
/**
15+
* @var string|null
16+
*/
17+
protected ?string $order;
18+
/**
19+
* @var string|null
20+
*/
21+
protected ?string $orderDirection;
22+
23+
/**
24+
* List all customer constructor.
25+
*
26+
* @param string|null $status
27+
* @param string|null $order
28+
* @param string|null $orderDirection
29+
*/
30+
public function __construct(string $status = null, string $order = null, string $orderDirection = null)
31+
{
32+
$this->status = $status;
33+
$this->order = $order;
34+
$this->orderDirection = $orderDirection;
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function handle(): array
41+
{
42+
$customer = new Customer();
43+
44+
if (! is_null($this->status)) {
45+
$customer->where('status', $this->status);
46+
}
47+
48+
if (! (is_null($this->order) && is_null($this->orderDirection))) {
49+
$customer->orderBy($this->order, $this->orderDirection);
50+
}
51+
52+
return $customer->all();
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Domains\Subscriptions\Validations\Customer;
4+
5+
use App\Support\Validation\Validation;
6+
7+
class CreateNewCustomerValidation extends Validation
8+
{
9+
10+
protected function rules(): array
11+
{
12+
return [
13+
'name' => 'required',
14+
'lastname' => 'required',
15+
'email' => 'required|unique:customers,email',
16+
'status' => [
17+
'required',
18+
'in:ACTIVE,INACTIVE', // real use: Rule::in(...)
19+
]
20+
];
21+
}
22+
23+
protected function messages(): array
24+
{
25+
return [];
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Domains\Subscriptions\Services\ListAllCustomers;
6+
use App\Illuminate\Http\Request;
7+
use App\Support\Validation\ValidationException;
8+
use App\Domains\Subscriptions\Services\CreateNewCustomer;
9+
10+
class CustomerController
11+
{
12+
public static function index(Request $request) : array
13+
{
14+
return (new ListAllCustomers(
15+
$request->status,
16+
$request->order,
17+
$request->orderDirection
18+
))->handle();
19+
}
20+
21+
public static function store(Request $request)
22+
{
23+
try {
24+
$customer = new CreateNewCustomer(
25+
$request->nome,
26+
$request->sobrenome,
27+
$request->email,
28+
$request->status,
29+
);
30+
$customer->handle();
31+
} catch (ValidationException $e) {
32+
return response($e->all(), 422);
33+
}
34+
35+
return response(null, 201);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Domains\Subscriptions\Services\CreateNewSubscription;
6+
use App\Domains\Subscriptions\Services\CreateNewSubscriptionWithNewCustomer;
7+
use App\Support\Validation\ValidationException;
8+
9+
class SubscriptionController
10+
{
11+
public static function createNewSubscription(\stdClass $request)
12+
{
13+
try {
14+
$subscription = new CreateNewSubscription($request->customer_id, $request->plan_id);
15+
$subscription = $subscription->handle();
16+
response($subscription->toArray(), 201);
17+
} catch (ValidationException $e) {
18+
response($e->all(), 201);
19+
}
20+
}
21+
22+
public static function createNewSubscriptionWithNewSubscriber(\stdClass $request)
23+
{
24+
try {
25+
$subscription = (new CreateNewSubscriptionWithNewCustomer(
26+
$request->customer,
27+
$request->plan_id
28+
))->handle();
29+
30+
response($subscription, 201);
31+
} catch (ValidationException $e) {
32+
response($e->all(), 201);
33+
}
34+
}
35+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Support\Services;
4+
5+
interface ServiceInterface
6+
{
7+
/**
8+
* @return array|string|int|bool
9+
*/
10+
public function handle();
11+
}

0 commit comments

Comments
 (0)