Skip to content

Commit 608a6e1

Browse files
committed
- send scheduled email
1 parent b3661f0 commit 608a6e1

File tree

5 files changed

+112
-76
lines changed

5 files changed

+112
-76
lines changed

bootstrap/function.php

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
if (!function_exists('env')) {
44
/**
5-
* @param $property
6-
* @param null $default
5+
* @param string $property
6+
* @param mixed $default (null)
77
* @return string
88
*/
99
function env($property, $default = null)
@@ -22,7 +22,7 @@ function env($property, $default = null)
2222
if (!function_exists('path')) {
2323

2424
/**
25-
* @param $root
25+
* @param string $root
2626
* @return string
2727
*/
2828
function path($root)
@@ -41,12 +41,34 @@ function path($root)
4141
}
4242
}
4343

44+
if (!function_exists('storage')) {
45+
/**
46+
* @param string $path
47+
* @return string
48+
*/
49+
function storage($path)
50+
{
51+
return path(true, 'storage', $path);
52+
}
53+
}
54+
55+
if (!function_exists('resources')) {
56+
/**
57+
* @param string $path
58+
* @return string
59+
*/
60+
function resources($path)
61+
{
62+
return path(true, 'app/resources', $path);
63+
}
64+
}
65+
4466
if (!function_exists('out')) {
4567
/**
4668
*
47-
* @param $value
48-
* @param bool $print
49-
* @param string $type
69+
* @param mixed $value
70+
* @param bool $print (true)
71+
* @param string $type (null)
5072
* @return string
5173
*/
5274
function out($value, $print = true, $type = null)
@@ -81,8 +103,8 @@ function out($value, $print = true, $type = null)
81103

82104
if (!function_exists('of')) {
83105
/**
84-
* @param $value
85-
* @param $default
106+
* @param mixed $value
107+
* @param mixed $default (false)
86108
* @return mixed
87109
*/
88110
function of($value, $default = false)
@@ -93,9 +115,9 @@ function of($value, $default = false)
93115

94116
if (!function_exists('off')) {
95117
/**
96-
* @param $value
97-
* @param $property
98-
* @param $default
118+
* @param mixed $value
119+
* @param mixed $property (null)
120+
* @param mixed $default (null)
99121
*
100122
* @return mixed
101123
*/
@@ -116,7 +138,7 @@ function off($value, $property = null, $default = null)
116138

117139
if (!function_exists('stop')) {
118140
/**
119-
*
141+
* @die
120142
*/
121143
function stop()
122144
{
@@ -129,11 +151,10 @@ function stop()
129151
}
130152
}
131153

132-
133154
if (!function_exists('config')) {
134155
/**
135-
* @param $name
136-
* @return object
156+
* @param string $name
157+
* @return mixed
137158
*/
138159
function config($name)
139160
{
@@ -143,8 +164,8 @@ function config($name)
143164

144165
if (!function_exists('headerify')) {
145166
/**
146-
* @param $name
147-
* @return mixed
167+
* @param string $name
168+
* @return string
148169
*/
149170
function headerify($name)
150171
{
@@ -154,10 +175,10 @@ function headerify($name)
154175

155176
if (!function_exists('str_replace_first')) {
156177
/**
157-
* @param $from
158-
* @param $to
159-
* @param $subject
160-
* @param $quote
178+
* @param string $from
179+
* @param string $to
180+
* @param string $subject
181+
* @param bool $quote (false)
161182
* @return mixed
162183
*/
163184
function str_replace_first($from, $to, $subject, $quote = false)
@@ -193,7 +214,7 @@ function guid($brackets = false)
193214

194215
if (!function_exists('is_iterator')) {
195216
/**
196-
* @param $var
217+
* @param mixed $var
197218
* @return bool
198219
*/
199220
function is_iterator($var)
@@ -215,8 +236,8 @@ function throw_format(Throwable $throw)
215236

216237
if (!function_exists('search')) {
217238
/**
218-
* @param $context
219-
* @param $path
239+
* @param mixed $context
240+
* @param array|string $path
220241
* @return mixed|null
221242
*/
222243
function search($context, $path)

src/Helper/Directory.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Simples\Core\Helper;
44

55
use FilesystemIterator;
6+
use DirectoryIterator;
67

78
/**
89
* Class Directory
@@ -11,28 +12,28 @@
1112
abstract class Directory
1213
{
1314
/**
14-
* @param $dir
15+
* @param string $dir
1516
* @return int
1617
*/
17-
public static function count($dir)
18+
public static function count(string $dir): int
1819
{
19-
return iterator_count(new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS));
20+
return (int)iterator_count(new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS));
2021
}
2122

2223
/**
23-
* @param $dir
24+
* @param string $dir
2425
* @return bool
2526
*/
26-
public static function exists($dir)
27+
public static function exists(string $dir): bool
2728
{
2829
return is_dir($dir);
2930
}
3031

3132
/**
32-
* @param $dir
33+
* @param string $dir
3334
* @return bool
3435
*/
35-
public static function make($dir)
36+
public static function make(string $dir): bool
3637
{
3738
$make = is_dir($dir);
3839
if (!$make) {
@@ -42,10 +43,10 @@ public static function make($dir)
4243
}
4344

4445
/**
45-
* @param $dir
46+
* @param string $dir
4647
* @return bool
4748
*/
48-
public static function remove($dir)
49+
public static function remove(string $dir): bool
4950
{
5051
$files = array_diff(scandir($dir), ['.', '..']);
5152
foreach ($files as $file) {
@@ -55,25 +56,25 @@ public static function remove($dir)
5556
}
5657

5758
/**
58-
* @param $source
59-
* @param $target
59+
* @param string $source
60+
* @param string $target
6061
* @return bool
6162
*/
62-
public static function rename($source, $target)
63+
public static function rename(string $source, string $target): bool
6364
{
6465
return rename($source, $target);
6566
}
6667

6768
/**
68-
* @param $dir
69+
* @param string $dir
6970
* @return array
7071
*/
71-
public static function getFiles($dir)
72+
public static function getFiles(string $dir): array
7273
{
7374
$files = [];
7475

7576
if (self::exists($dir)) {
76-
foreach (new \DirectoryIterator($dir) as $fileInfo) {
77+
foreach (new DirectoryIterator($dir) as $fileInfo) {
7778
if ($fileInfo->isDot()) {
7879
continue;
7980
}

src/Message/Lang.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,10 @@ public static function __callStatic($name, $arguments)
4646
public static function lang($scope, $path, array $parameters = [])
4747
{
4848
$i18n = "Lang '{$scope}.{$path}' not found";
49-
5049
$languages = App::options('lang');
50+
$filename = static::filename($scope, $languages['default'], $languages['fallback']);
5151

52-
$filename = path(true, "app/resources/locales/{$languages['default']}/{$scope}.php");
53-
if (!File::exists($filename)) {
54-
$filename = path(true, "app/resources/locales/{$languages['fallback']}/{$scope}.php");
55-
}
56-
57-
if (File::exists($filename)) {
58-
52+
if ($filename) {
5953
/** @noinspection PhpIncludeInspection */
6054
$phrases = include $filename;
6155

@@ -79,4 +73,22 @@ public static function replace($i18n, $parameters)
7973
}
8074
return $i18n;
8175
}
76+
77+
/**
78+
* @param string $scope
79+
* @param string $default
80+
* @param string $fallback
81+
* @return string
82+
*/
83+
private static function filename(string $scope, string $default, string $fallback): string
84+
{
85+
$filename = resources("locales/{$default}/{$scope}.php");
86+
if (!File::exists($filename)) {
87+
$filename = resources("locales/{$fallback}/{$scope}.php");
88+
}
89+
if (File::exists($filename)) {
90+
return $filename;
91+
}
92+
return '';
93+
}
8294
}

src/Message/Mail.php

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,7 @@ class Mail
8181
/**
8282
* @var string
8383
*/
84-
const STATUS_WAITING = 'waiting';
85-
86-
/**
87-
* @var string
88-
*/
89-
const STATUS_SENT = 'sent';
90-
91-
/**
92-
* @var string
93-
*/
94-
const STATUS_ERROR = 'error';
84+
const STATUS_WAITING = 'waiting', STATUS_SENT = 'sent', STATUS_ERROR = 'error';
9585

9686
/**
9787
* EMail constructor.
@@ -103,7 +93,7 @@ class Mail
10393
* @param string $fromAddress
10494
* @param string $fromName
10595
*/
106-
public function __construct($subject, $message, $toAddress, $toName = '', $alt = '', $fromAddress = '', $fromName = '')
96+
public function __construct($subject = '', $message = '', $toAddress = '', $toName = '', $alt = '', $fromAddress = '', $fromName = '')
10797
{
10898
$this->subject = $subject;
10999
$this->message = $message;
@@ -126,7 +116,7 @@ public function send($driver = 'default')
126116
if ($this->toAddress) {
127117
$file = $this->id . '.' . 'mail';
128118

129-
$root = path(true, 'storage/files/mail');
119+
$root = storage('files/mail');
130120

131121
$waiting = path($root, self::STATUS_WAITING, $file);
132122
if (File::exists($waiting)) {
@@ -192,39 +182,35 @@ public function send($driver = 'default')
192182
}
193183

194184
/**
195-
* @return int|null
185+
* @return bool
196186
*/
197-
public function schedule()
187+
public function schedule(): bool
198188
{
199-
$filename = path(true, 'storage/files/mail', self::STATUS_WAITING, $this->id . '.' . 'mail');
189+
$filename = storage('files/mail/' . self::STATUS_WAITING . '/' . $this->id . '.' . 'mail');
200190
if (!File::exists($filename)) {
201191
if (File::write($filename, $this->json())) {
202-
return $this->id;
192+
return true;
203193
}
204194
}
205-
return null;
195+
return false;
206196
}
207197

208198
/**
209-
* @param $id
210-
* @param string $status
211-
* @return mixed
199+
* @param $filename
200+
* @return Mail
212201
*/
213-
public function load($id, $status = null)
202+
public static function load($filename): Mail
214203
{
215-
$status = of($status, self::STATUS_WAITING);
216-
217-
$filename = path(true, 'storage', 'files', 'mail', $status, $id . '.' . 'mail');
218-
204+
$instance = new static();
219205
if (File::exists($filename)) {
220206
$properties = Json::decode(File::read($filename));
221207
foreach ($properties as $key => $value) {
222208
/** @noinspection PhpVariableVariableInspection */
223-
$this->$key = $value;
209+
$instance->$key = $value;
224210
}
225211
}
226212

227-
return $this;
213+
return $instance;
228214
}
229215

230216
/**

0 commit comments

Comments
 (0)