Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ProductService
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
6 / 6
8
100.00% covered (success)
100.00%
1 / 1
 createProduct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 getAllVendorCodes
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 deleteProduct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 searchProducts
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getProductById
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 updateProduct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace App\Service;
3
4use PDO;
5
6class ProductService
7{
8    public static function createProduct(PDO $pdo, array $data): int
9    {
10        $sql = '
11            INSERT INTO products (product_code, product_name, price, stock_quantity, vendor_code)
12            VALUES (:product_code, :product_name, :price, :stock_quantity, :vendor_code)';
13        $stmt = $pdo->prepare($sql);
14        $stmt->bindValue(':product_code', $data['product_code'], PDO::PARAM_INT);
15        $stmt->bindValue(':product_name', $data['product_name'], PDO::PARAM_STR);
16        $stmt->bindValue(':price', $data['price'], PDO::PARAM_INT);
17        $stmt->bindValue(':stock_quantity', $data['stock_quantity'], PDO::PARAM_INT);
18        $stmt->bindValue(':vendor_code', $data['vendor_code'], PDO::PARAM_INT);
19        $stmt->execute();
20        return $stmt->rowCount();
21    }
22
23    public static function getAllVendorCodes(PDO $pdo): array
24    {
25        $sql = 'SELECT vendor_code FROM vendors';
26        $stmt = $pdo->query($sql);
27        return $stmt->fetchAll(PDO::FETCH_COLUMN);
28    }
29
30    public static function deleteProduct(PDO $pdo, int $id): int
31    {
32        $sql = 'DELETE FROM products WHERE id = :id';
33        $stmt = $pdo->prepare($sql);
34        $stmt->bindValue(':id', $id, PDO::PARAM_INT);
35        $stmt->execute();
36        return $stmt->rowCount();
37    }
38
39    /**
40     * 商品一覧取得(部分一致検索&並び順)
41     * 
42     * @param PDO    $pdo
43     * @param string $keyword  検索ワード(空OK)
44     * @param string $order    並び順('asc'|'desc'以外ならasc)
45     * @return array
46     */
47    public static function searchProducts(PDO $pdo, string $keyword = '', string $order = 'asc'): array
48    {
49        $order = ($order === 'desc') ? 'DESC' : 'ASC';
50        $sql = 'SELECT * FROM products WHERE product_name LIKE :keyword ORDER BY updated_at ' . $order;
51        $stmt = $pdo->prepare($sql);
52        // 検索語の前後に%をつける(部分一致)
53        $partial_match = "%" . $keyword . "%";
54        $stmt->bindValue(':keyword', $partial_match, PDO::PARAM_STR);
55        $stmt->execute();
56        return $stmt->fetchAll(PDO::FETCH_ASSOC);
57    }
58
59    public static function getProductById(PDO $pdo, int $id): ?array
60    {
61        $sql = 'SELECT * FROM products WHERE id = :id';
62        $stmt = $pdo->prepare($sql);
63        $stmt->bindValue(':id', $id, PDO::PARAM_INT);
64        $stmt->execute();
65        $result = $stmt->fetch(PDO::FETCH_ASSOC);
66        return $result === false ? null : $result;
67    }
68
69    // 商品情報を更新
70    public static function updateProduct(PDO $pdo, int $id, array $data): int
71    {
72        $sql = '
73            UPDATE products
74            SET
75                product_code = :product_code,
76                product_name = :product_name,
77                price = :price,
78                stock_quantity = :stock_quantity,
79                vendor_code = :vendor_code
80            WHERE id = :id
81        ';
82        $stmt = $pdo->prepare($sql);
83        $stmt->bindValue(':product_code', $data['product_code'], PDO::PARAM_INT);
84        $stmt->bindValue(':product_name', $data['product_name'], PDO::PARAM_STR);
85        $stmt->bindValue(':price', $data['price'], PDO::PARAM_INT);
86        $stmt->bindValue(':stock_quantity', $data['stock_quantity'], PDO::PARAM_INT);
87        $stmt->bindValue(':vendor_code', $data['vendor_code'], PDO::PARAM_INT);
88        $stmt->bindValue(':id', $id, PDO::PARAM_INT);
89        $stmt->execute();
90        return $stmt->rowCount();
91    }
92    
93}