Completed
Branch FET/attendee-calculated-fields (ad39bd)
by
unknown
40:43 queued 26:47
created

Attendee::getAttendeeObject()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8

Duplication

Lines 3
Ratio 37.5 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 1
dl 3
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\libraries\rest_api\calculations;
4
5
use EE_Attendee;
6
use EE_Error;
7
use EEM_Attendee;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use EventEspresso\core\libraries\rest_api\calculations\Base as Calculations_Base;
11
use EventEspresso\core\libraries\rest_api\controllers\model\Base;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, EventEspresso\core\libra...t_api\calculations\Base.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use InvalidArgumentException;
13
use WP_REST_Request;
14
15
/**
16
 * Class Attendee
17
 * adds calculated fields to the  REST API output for the /attendees/ endpoint
18
 *
19
 * @package EventEspresso\core\libraries\rest_api\calculations
20
 * @author  Brent Christensen
21
 * @since   $VID:$
22
 */
23
class Attendee extends Calculations_Base
24
{
25
26
    /**
27
     * @since $VID:$
28
     * @param $wpdb_row
29
     * @return EE_Attendee|\EE_Base_Class
30
     * @throws EE_Error
31
     * @throws InvalidArgumentException
32
     * @throws InvalidDataTypeException
33
     * @throws InvalidInterfaceException
34
     */
35
    private static function getAttendeeObject($wpdb_row)
36
    {
37
        $attendee = null;
38 View Code Duplication
        if (is_array($wpdb_row) && isset($wpdb_row['Attendee_CPT.ID']) && absint($wpdb_row['Attendee_CPT.ID'])) {
39
            $attendee = EEM_Attendee::instance()->get_one_by_ID($wpdb_row['Attendee_CPT.ID']);
40
        }
41
        return $attendee;
42
    }
43
44
45
    /**
46
     * @param array           $wpdb_row
47
     * @param WP_REST_Request $request
48
     * @param Base            $controller
49
     * @since $VID:$
50
     * @return string
51
     * @throws EE_Error
52
     * @throws InvalidArgumentException
53
     * @throws InvalidDataTypeException
54
     * @throws InvalidInterfaceException
55
     */
56
    public static function userAvatar(array $wpdb_row, WP_REST_Request $request, Base $controller)
57
    {
58
        $attendee = Attendee::getAttendeeObject($wpdb_row);
59
        if (! $attendee instanceof EE_Attendee) {
60
            return '';
61
        }
62
        $avatar = get_avatar_url($attendee->email());
63
        return $avatar ? $avatar : '';
64
    }
65
}
66