Completed
Pull Request — master (#4)
by Jacques
24:15 queued 09:15
created

AsylumExpirationTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C is_valid() 0 30 7
1
<?php
2
/**
3
 * PHP Validators
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the MIT license.
19
 *
20
 * @author    Jacques Marneweck <[email protected]>
21
 * @copyright 2002-2017 Jacques Marneweck.  All rights strictly reserved.
22
 * @license   MIT
23
 */
24
25
namespace Jacques\Validators;
26
27
use Carbon\Carbon;
28
29
class AsylumExpirationTest
30
{
31
    public static function is_valid($asylum_expiration_date = null)
32
    {
33
        if (is_null($asylum_expiration_date)) {
34
            throw new \InvalidArgumentException('Please pass in the date that the asylum document expires on.');
35
        }
36
37
        if (empty(trim($asylum_expiration_date))) {
38
            throw new \InvalidArgumentException('Please pass in the date that the asylum document expires on.');
39
        }
40
41
        /**
42
         * Rule from Tim here is that he wants a asylum document to still be used
43
         * to register a user on the date their asylum document expires.
44
         */
45
        try {
46
            $date = Carbon::createFromFormat('Y-m-d', $asylum_expiration_date, 'UTC')->startOfDay();
47
            if ($date->toDateString() === $asylum_expiration_date) {
48
                $now = Carbon::now()->startOfDay();
49
                if ($date->gte($now)) {
50
                    return true;
51
                }
52
            }
53
        } catch (\Exception $e) {
54
            if ('Data missing' == $e->getMessage()) {
55
                throw new \Exception('Please provide a asylum expiration data in YYYY-MM-DD format.');
56
            }
57
        }
58
59
        return false;
60
    }
61
}
62