Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.59% covered (success)
92.59%
25 / 27
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Dashboard_Threats
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 format
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
4
 format_all
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Shapes threats for the `@automattic/jetpack-scan` threat list.
4 *
5 * @package automattic/jetpack-protect
6 */
7
8namespace Automattic\Jetpack\Protect;
9
10if ( ! defined( 'ABSPATH' ) ) {
11    exit( 0 );
12}
13
14/**
15 * Converts Protect threat models into the camelCase shape the JS threat list reads.
16 *
17 * @since $$next-version$$
18 */
19class Dashboard_Threats {
20
21    /**
22     * Shape one threat.
23     *
24     * @param object $threat A Threat_Model, or a raw threat with the same properties.
25     * @return array
26     */
27    public static function format( $threat ) {
28        $extension = $threat->extension ?? null;
29
30        return array(
31            'id'            => $threat->id ?? null,
32            'signature'     => $threat->signature ?? null,
33            'title'         => $threat->title ?? null,
34            'description'   => $threat->description ?? null,
35            'status'        => $threat->status ?? null,
36            'severity'      => $threat->severity ?? null,
37            'firstDetected' => $threat->first_detected ?? null,
38            'fixedIn'       => $threat->fixed_in ?? null,
39            'fixedOn'       => $threat->fixed_on ?? null,
40            'fixable'       => empty( $threat->fixable ) ? false : $threat->fixable,
41            'filename'      => $threat->filename ?? null,
42            'extension'     => $extension ? array(
43                'slug'    => $extension->slug ?? null,
44                'name'    => $extension->name ?? null,
45                'version' => $extension->version ?? null,
46                // History names extension types in the singular; the threat list reads the plural.
47                'type'    => in_array( $extension->type ?? '', array( 'plugin', 'theme' ), true ) ? $extension->type . 's' : ( $extension->type ?? null ),
48            ) : null,
49        );
50    }
51
52    /**
53     * Shape a list of threats.
54     *
55     * @param iterable $threats Threats, as an array or a Traversable.
56     * @return array
57     */
58    public static function format_all( $threats ) {
59        $formatted = array();
60        if ( ! is_iterable( $threats ) ) {
61            return $formatted;
62        }
63        foreach ( $threats as $threat ) {
64            $formatted[] = self::format( $threat );
65        }
66        return $formatted;
67    }
68}