• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tattersoftware / codeigniter4-forms / 7561307112

17 Jan 2024 08:17PM UTC coverage: 81.915%. First build
7561307112

Pull #27

github

web-flow
Merge 892323ee6 into 76efa679d
Pull Request #27: Bump actions/cache from 3 to 4

77 of 94 relevant lines covered (81.91%)

4.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Controllers/ResourceController.php
1
<?php
2

3
namespace Tatter\Forms\Controllers;
4

5
use CodeIgniter\HTTP\ResponseInterface;
6
use CodeIgniter\RESTful\ResourceController as BaseController;
7
use Tatter\Forms\Traits\ResourceTrait;
8

9
abstract class ResourceController extends BaseController
10
{
11
    use ResourceTrait;
12

13
    //--------------------------------------------------------------------
14
    // CRUD Methods
15
    //--------------------------------------------------------------------
16

17
    public function create()
18
    {
19
        $data = $this->request->getPost();
2✔
20

21
        if (! $id = $this->model->insert($data)) {
2✔
22
            return $this->actionFailed('create', 422);
1✔
23
        }
24

25
        return $this->respondCreated($this->model->find($id), lang('Forms.created', [ucfirst($this->name)]));
1✔
26
    }
27

28
    public function index()
29
    {
30
        return $this->respond($this->model->findAll());
1✔
31
    }
32

33
    public function show($id = null)
34
    {
35
        if (($object = $this->ensureExists($id)) instanceof ResponseInterface) {
3✔
36
            return $object;
2✔
37
        }
38

39
        return $this->respond($object);
1✔
40
    }
41

42
    public function update($id = null)
43
    {
44
        if (($object = $this->ensureExists($id)) instanceof ResponseInterface) {
4✔
45
            return $object;
2✔
46
        }
47

48
        $data = $this->request->getPost();
2✔
49

50
        if (! $this->model->update($id, $data)) {
2✔
51
            return $this->actionFailed('update', 422);
1✔
52
        }
53

54
        return $this->respond($this->model->find($id), 200, lang('Forms.updated', [ucfirst($this->name)]));
1✔
55
    }
56

57
    public function delete($id = null)
58
    {
59
        if (($object = $this->ensureExists($id)) instanceof ResponseInterface) {
4✔
60
            return $object;
2✔
61
        }
62

63
        if (! $this->model->delete($id)) {
2✔
64
            return $this->actionFailed('delete');
1✔
65
        }
66

67
        return $this->respondDeleted($object, lang('Forms.deleted', [ucfirst($this->name)]));
1✔
68
    }
69

70
    //--------------------------------------------------------------------
71
    // Support Methods
72
    //--------------------------------------------------------------------
73

74
    /**
75
     * Fetches an object or returns a failure Response.
76
     *
77
     * @param int|string|null $id
78
     *
79
     * @return mixed
80
     */
81
    protected function ensureExists($id = null)
82
    {
83
        if (isset($id) && $object = $this->model->withDeleted()->find($id)) {
11✔
84
            return $object;
5✔
85
        }
86

87
        return $this->failNotFound('Not Found', null, lang('Forms.notFound', [ucfirst($this->name)]));
6✔
88
    }
89

90
    /**
91
     * Creates a standardized failure Response.
92
     *
93
     * @return ResponseInterface
94
     */
95
    protected function actionFailed(string $action, int $status = 400)
96
    {
97
        $errors = $this->model->errors() ?: [
3✔
98
            lang("Forms.{$action}Failed", [ucfirst($this->name)]),
3✔
99
        ];
3✔
100

101
        $response = [
3✔
102
            'status'   => $status,
3✔
103
            'error'    => ucfirst($action) . ' Failed',
3✔
104
            'messages' => $errors,
3✔
105
        ];
3✔
106

107
        return $this->respond($response, $status);
3✔
108
    }
109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc