Traceable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 11
dl 0
loc 31
rs 10
c 6
b 1
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTracer() 0 3 1
A TraceableTraitor_after_save() 0 14 2
A TraceableTraitor_after_destroy() 0 2 1
1
<?php
2
3
namespace HexMakina\kadro\Controllers\Abilities;
4
5
use HexMakina\BlackBox\Database\TracerInterface;
6
use HexMakina\Tracer\Trace;
7
use HexMakina\BlackBox\ORM\ModelInterface;
8
use HexMakina\BlackBox\Auth\OperatorInterface;
9
use Psr\Container\ContainerInterface;
10
11
trait Traceable
12
{
13
14
    abstract public function formModel(): ModelInterface;
15
    abstract public function loadModel(): ?ModelInterface;
16
    abstract public function operator(): OperatorInterface;
17
18
19
    public function getTracer(): TracerInterface
20
    {
21
        return $this->get('HexMakina\BlackBox\Database\TracerInterface');
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        return $this->/** @scrutinizer ignore-call */ get('HexMakina\BlackBox\Database\TracerInterface');
Loading history...
22
    }
23
24
    public function TraceableTraitor_after_save()
25
    {
26
        $trace = new Trace();
27
        $trace->tableName(get_class($this->formModel())::relationalMappingName());
28
29
        if (is_null($this->loadModel())) {
30
            $trace->isInsert(true);
31
        } else {
32
            $trace->isUpdate(true);
33
        }
34
        $trace->tablePk($this->formModel()->getId());
35
        $trace->operatorId($this->operator()->getId());
36
37
        $this->getTracer()->trace($trace);
38
    }
39
40
    public function TraceableTraitor_after_destroy()
41
    {
42
        ;
43
    }
44
45
46
47
  // public function trace(TracerInterface $tracer, ModelInterface $model, OperatorInterface $op)
48
  // {
49
  //   $tracer->trace($model->last_alter_query, $op->getId(), $model->getId());
50
  //
51
  // }
52
53
  // public function traces($options = []) : array
54
  // {
55
  //   $options['query_table'] = get_class($this)::relationalMappingName();
56
  //   $options['query_id'] = $this->get_model_id();
57
  //   return $this->get_tracer()->traces($options);
58
  //
59
  //   // $q = $this->get_tracer()->tracing_table()->select();
60
  //   // $q->whereFieldsEQ(['query_table' => get_class($this)::relationalMappingName(), 'query_id' => $this->getId()]);
61
  //   // $q->orderBy(['query_on', 'DESC']);
62
  //   // $q->run();
63
  //   // $res = $q->retAss();
64
  //   //
65
  //   // return $res;
66
  // }
67
68
  // don't really know the purpose of this anymore.. came from Tracer
69
  // public function traces_by_model(ModelInterface $m)
70
  // {
71
  //     return $this->get_tracer()->traces(['id' => $m->getId(), 'table' => get_class($m)::relationalMappingName()]);
72
  // }
73
}
74